Search code examples
androidcursor

Sort Cursor by 3 columns


How can I use a Cursor with nBD.query to ORDER BY column1 ASC, then column2 ASC and finally by column3 in order ASC too.

Cursor D = nBD.query(NOMBRE_TB, columnas, null, null, null, null, column1, null);

In SQL is:

Cursor c = nBD.rawQuery("SELECT * FROM " + NOMBRE_TB + " ORDER BY " + COLUMN1 + " ASC, " + COLUMN2 + " ASC, " + COLUMN3 + " ASC", null); 

but I'd like to use the cursor D.


Solution

  • You need to add multiple Order By clause in a single string

    String order = "column1 ASC, column2 ASC, column3 ASC";
    Cursor D = nBD.query(NOMBRE_TB, columnas, null, null, null, null, order, null);
    

    replace column with real column names. This will make your query to be sorted in way you desire.