Noob
Noob

Reputation: 145

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)

Upvotes: 0

Views: 681

Answers (2)

Noob
Noob

Reputation: 145

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

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007359

If I am reading the SQLite documentation correctly, date() expects a value of the form %Y-%m-%d. Yours is not.

If you are using two-digit minute and second values, this should sort without any function.

(above was from original question, not the update)

Also, you are querying a ContentProvider. There is no requirement that a ContentProvider offer sorting. If this is your own ContentProvider, you need to check on its side whether or not it is passing the sort clause on to SQLite or whatever your backing store is.

Upvotes: 1

Related Questions