Search code examples
androidsortingandroid-cursor

How to sort a cursor based on a time string


My question if fairly simple, I have this segment of code

Cursor mCursor = this.getContentResolver().query(
            PlayerContentProviderDB.CONTENT_URI, fulldataColumns, null, null,
            Players.SCORE +" ASC");

This code, from everything I've seen, should be sorting the cursor in ascending order based on Players.SCORE but it is not.

For reference Players.SCORE will always be a string in the format
mm:ss:msmsms ( read minute:second:millisecond).

Also here is the code for the query method in my ContentProvider

public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    checkColumns(projection);
    queryBuilder.setTables(PlayerContract.Players.PLAYERS_TABLE_NAME);
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case ALL_SCORES:
    break;
    case SCORE_ID:
    queryBuilder.appendWhere(PlayerContract.Players.ID + "="
    + uri.getLastPathSegment());
    break;
    default:
    throw new IllegalArgumentException("Unknown URI: " + uri);
    }
    SQLiteDatabase db = this.dbHelper.getReadableDatabase(); 
    Cursor cursor = queryBuilder.query(db, projection, selection,
    selectionArgs, null, null, sortOrder);
    // Notify potential listeners
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;

My question is why dose this code not work and how can I fix it?

(Another interesting note about this code is that I am also trying to add a limit statement to the end of the sort, which I have read works, however mine is not)


Solution

  • I finally found the answer to this question. As it turns out my code was correct, however for some reason that I am still working on my ContentProvider.query() was being called twice. Once with the correct parameters and again with incorrect parameters.(i.e: sortOrder == null).

    Also note that you can not sort a string of numbers in numerical order as sql sorts ints and strings differently