Search code examples
realmrealm-list

How to sort the RealmResults with recents dates?


I have around 20 rows in RealmResults and need to sort the list with recent dates

RealmConfiguration realmConfig = new RealmConfiguration.Builder(getActivity()).build();
Realm realm = Realm.getInstance(realmConfig);

Like below

RealmResults<MyTable> List = realm.where(MyTable.class).findAll().sort("date",SORT.DESCENDING);

Solution

  • It's really just the following.

    RealmResults<MyTable> list = realm.where(MyTable.class)
                                    .findAllSorted("date",Sort.DESCENDING);
    

    And since 4.3.x:

    RealmResults<MyTable> list = realm.where(MyTable.class)
                          .sort("date",Sort.DESCENDING)
                          .findAll();