Adariel Lzinski
Adariel Lzinski

Reputation: 1121

Sort database list alphabetically [sql]

I'm having trouble sorting my database alphabetically. I assume I need to sort the database here:

public static Cursor getAllRecords() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
            null, null, null, null, null, null);
}

I've tried replacing the last null with " DESC" which didn't work.
Am I doing this in the wrong spot?

Upvotes: 1

Views: 1016

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

Try this -

return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM}, null, null, null, null, KEY_ITEM+ " ASC");

or

return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM}, null, null, null, null, KEY_ITEM+ " DESC");

Upvotes: 1

flx
flx

Reputation: 14226

Use something like KEY_ITEM + " DESC" for the orderBy parameter:

public static Cursor getAllRecords() {
    return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_ITEM},
            null, null, null, null, KEY_ITEM + " DESC");  // note the missing last null for 'limit'
}

Upvotes: 2

Related Questions