Search code examples
androidsortingandroid-sqlitecomparableandroid-cursor

How to apply own sorting logic for elements display in ListView?


I use standard approach to display my database records in the ListView:

recordsCursor = mDb.query(DATABASE_BL_TABLE, new String[] {KEY_BL_ROWID, KEY_BL_SENDER, KEY_BL_ADDED}, null, null, null, null, KEY_BL_SENDER);
startManagingCursor(recordsCursor);

String[] from = new String[]{DbAdapter.KEY_BL_SENDER};
int[] to = new int[]{R.id.text1};
adapter = new SimpleCursorAdapter(this, R.layout.mylist_row, recordsCursor, from, to);
setListAdapter(adapter);

So, all records are sorted by KEY_BL_SENDER. I would like to implement the following sorting logic:

  1. ignore case (so, the list of 4 elements - aa, bb, AA, BB will be displayed in the following order: AA, aa, BB, bb);
  2. ignore other symbols besides letters (the list of 4 elements - ** aa **, ~bb~, aa, __BB__ will be displayed in the following order: aa, ** aa **, __BB__, ~bb~).

How could I do it?


Solution

  • Let's join some known solutions:

    Your query would look like:

    SELECT ... 
    FROM ... 
    WHERE ... 
    ORDER BY 
        TRIM(KEY_BL_SENDER, "_*~ #$") 
        COLLATE NOCASE ASC;