javaandroidrealmrealm-java

Android realm sorting list of objects


How I can sort list of RealmObjects by two parameters? For example first param is name (Need to sort it alphabetically), second parameter is a bit complicated and related to 3 dates: I have a range dates (for example fromDate currentDate and toDate). Need to put to the head of the list all items which happening today. Also do not forget about alphabetically sorting.


Solution

  • You're basically trying to sort a realmList depending on multiple parameters. Here's the way to go:

    String[] fields = {"name","fromDate","currentDate","toDate"};
    Sort[] howToSort = {Sort.ASCENDING, Sort.ASCENDING, Sort.ASCENDING, Sort.ASCENDING};
    

    And then, you simply do a usual realm selection:

    realm.where(YourRealmObject.class).findAllSorted(fileds, howToSort);
    

    As @EpicPandaForce commented, please check the docs.