I need to query a database with a sorted order by the name of an item and date of an item.
for example first I want to get all the names of the items in alphabetical order and thats no problem but there can be multiple items with the same name so I also want to sort them by the date that I insert them into the database like this
A 1/1/12,
A 1/2/12,
A 1/3/12,
B 1/1/12,
B 1/2/12,
B 1/3/12,
etc….
how can I do this?
All the sorting I have done was by just one field like this
CalendarEvents.EVENT_START + " COLLATE LOCALIZED ASC"
this is the database I want to sort
db.execSQL("CREATE TABLE " + EMGNOTE_TABLE + "(" + EMG_ID + " integer primary key autoincrement, " +
EMG_NOTE_ID + " INTEGER, " + EMG_CDID + " INTEGER, " + EMG_CENTER_POINT + " TEXT, " + EMG_COMPANY_NAME +
" TEXT, " + EMG_NAME + " TEXT, " + EMG_DESC + " TEXT, " + EMG_START_DATE + " TEXT, " + EMG_TTL + " TEXT, "
+ EMG_CENTER_LAT + " TEXT, " + EMG_CENTER_LON + " TEXT, " + EMG_LOCATION_TYPE + " TEXT, " + EMG_UPDATED + " TEXT, " + EMG_READ + " INTEGER);");
the field EMG_START_DATE would be the date i want to sort by
sort now looks like this
CursorLoader(getActivity(),CalendarEvents.EVENTS_URI,
new String[] {CalendarEvents.EVENT_ID,CalendarEvents.EVENT_READ,CalendarEvents.EVENT_SUBJECT,
CalendarEvents.EVENT_COMPANY_NAME,CalendarEvents.EVENT_START}
,null
,null
,CalendarEvents.EVENT_COMPANY_NAME+" COLLATE LOCALIZED ASC, julianday("+CalendarEvents.EVENT_START + ")" + "COLLATE LOCALIZED DESC");
You haven’t showed your DB schema so I will assume you have an
ITEM_NAMEcolumn and anITEM_DATEcolumns stored asTEXTin ISO8601 format.In that case you have to simply use this
orderByin your query:Update
According to the schema, it should be (I guess):