Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLiteDatabase query sorting order

Tags:

android

sqlite

I have data in my database like this:

Alice
anderson
Beatrice
benny
Carmen
calzone

Using this code:

mDb.query(DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
            KEY_NAME }, null, null,
            null, null, KEY_NAME+ " ASC");

or this code with the filter being "":

mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
        KEY_NAME }, KEY_NAME + " LIKE ?",
        new String[] { filter+"%" }, null, null, null,
        null);

It sorts the above data like this:

Alice
Beatrice
Carmen
anderson
benny
calzone

Is this normal behavior? How do I get it to sort to the above order where all the As and Bs and Cs are together? Thanks.

like image 343
Maurice Avatar asked Feb 01 '12 01:02

Maurice


People also ask

How do I sort ascending in SQLite?

Introduction to SQLite ORDER BY clause The ORDER BY clause comes after the FROM clause. It allows you to sort the result set based on one or more columns in ascending or descending order. In this syntax, you place the column name by which you want to sort after the ORDER BY clause followed by the ASC or DESC keyword.

How do I sort data in SQLite database?

Discussion: Use the ORDER BY keyword and the name of the column by which you want to sort. This way, you'll sort the data in ascending order by this column. You could also use the ASC keyword to make it clear that the order is ascending (the earliest date is shown first, the latest date is shown last, etc.).

How do I sort multiple columns in ascending order in SQL?

Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first. The column that is entered at first place will get sorted first and likewise.

Which statement selects all orders sorted down from high to low according to order number?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.


1 Answers

SQL uses ASCII location to sort so you'd use "COLLATE" in some fashion...

Something in the nature of:

ORDER BY YourColumn Collate NOCASE

or

ORDER BY YourColumn Collate SQL_Latin1_General_CP850_BIN
like image 86
kenyu73 Avatar answered Oct 19 '22 08:10

kenyu73