Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER BY Date - Realm (Android)

Tags:

android

realm

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)

like image 293
Abhilash Avatar asked Jun 09 '16 14:06

Abhilash


2 Answers

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();
like image 196
EpicPandaForce Avatar answered Oct 13 '22 23:10

EpicPandaForce


Use just "sort"! "findAllSorted" is deprecated!

io.realm.RealmQuery.findAllSorted(String) Since 4.3.0, now use RealmQuery.sort(String) then RealmQuery.findAll() Finds all objects that fulfill the query conditions and sorted by specific field name in ascending order. Sorting is currently limited to character sets in 'Latin Basic', 'Latin Supplement', 'Latin Extended A', 'Latin Extended B' (UTF-8 range 0-591). For other character sets, sorting will have no effect.

More details in: LINK

like image 37
Túlio Calazans Avatar answered Oct 13 '22 21:10

Túlio Calazans