ORDER BY Date - Realm (Android)

Abhilash picture Abhilash · Jun 9, 2016 · Viewed 10k times · Source

I've a column/field called last_message_time of type Date in my Table A. Suppose querying Table A returns x results. How do i sort these results based on dates inside last_message_time column.

Example, in SQLite we have ORDER BY date(dateColumn)

Answer

EpicPandaForce picture EpicPandaForce · Jun 9, 2016
RealmResults<A> sorted = realm.where(A.class)
                              .findAllSorted("last_message_time", Sort.ASCENDING);

EDIT: since Realm 4.3.0, the following is preferred:

RealmResults<A> sorted = realm.where(A.class)
                              .sort("last_message_time", Sort.ASCENDING) 
                              .findAll();