9

I have one realm list and i want to sort the list alphabetically.

Collections.sort(contacts, new java.util.Comparator<Contacts>() {
        @Override
        public int compare(Contacts l1, Contacts l2) {
            String s1 = l1.getName();
            String s2 = l2.getName();

            return s1.compareToIgnoreCase(s2);
        }
 });

But this logic not woking i am posting crash report below, please kindly go through it.

java.lang.UnsupportedOperationException: Replacing and element is not supported.
at io.realm.RealmResults$RealmResultsListIterator.set(RealmResults.java:826
at io.realm.RealmResults$RealmResultsListIterator.set(RealmResults.java:757)at java.util.Collections.sort(Collections.java:1909)

Please kindly go through my post and suggest me some solution.

Atiq
  • 14,435
  • 6
  • 54
  • 69
deeptimancode
  • 1,139
  • 4
  • 18
  • 40
  • Possible duplicate of [Sorting arraylist in alphabetical order (case insensitive)](http://stackoverflow.com/questions/5815423/sorting-arraylist-in-alphabetical-order-case-insensitive) – Vishal Patoliya ツ Sep 02 '16 at 10:44
  • 1
    public void sort(java.lang.String[] fieldNames, boolean[] sortAscending) . Check the API reference https://realm.io/docs/java/1.2.0/api/ or https://realm.io/docs/java/0.80.0/api/io/realm/RealmResults.html – Sreehari Sep 02 '16 at 10:49
  • Thanks @Stallion , the Sort.ASCENDING and Sort.DESCENDING will also work for alphabetical orders. i though it will only work for integers. – deeptimancode Sep 02 '16 at 10:51
  • @VishalPatoliya Yes sorting of normal arraylist using Comparator ,we can do , i am searching for a Realm query which will return list of sorted alphabetical RealmResults. – deeptimancode Sep 02 '16 at 10:53
  • @DeeptimanPattnaik If it worked I guess I can put it as answer and get an upvote :) – Sreehari Sep 02 '16 at 10:53

3 Answers3

23

Sorting in Realm is pretty straight forward.

Here is an example:

Let's assume you want to sort the contact list by name, you should sort it when your querying the results, you will get the results already sorted for you.

There are multiple ways to achieve this

Example:

1) Simple and Recommended way:
// for sorting ascending
RealmResults<Contacts> result = realm.where(Contacts.class).findAllSorted("name");

// sort in descending order
RealmResults<Contacts> result = realm.where(Contacts.class).findAllSorted("name", Sort.DESCENDING); 

Updated since realm 5.0.0

1) Simple and Recommended way:

// for sorting ascending, Note that the Sort.ASCENDING value is the default if you don't specify the order
RealmResults<Contacts> result = realm.where(Contacts.class).sort("name").findAll();

// sort in descending order
RealmResults<Contacts> result = realm.where(Contacts.class).sort("name", Sort. DESCENDING).findAll();

2) for unsorted results

If you are getting unsorted results you can sort them anywhere this way.

RealmResults<Contacts> result = realm.where(Contacts.class).findAll();
result = result.sort("name"); // for sorting ascending

// and if you want to sort in descending order
result = result.sort("name", Sort.DESCENDING);

Have a look here you will find very detailed examples about Realm querying, sorting and other useful usage.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atiq
  • 14,435
  • 6
  • 54
  • 69
3

Technically you should use the following:

RealmResults<Contacts> result = realm.where(Contacts.class)
                                     .findAllSorted("name", Sort.DESCENDING); 

It is recommended over findAll().sort().

Graham
  • 7,431
  • 18
  • 59
  • 84
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

Use this to get the sorting in SORT_ORDER_ASCENDING or SORT_ORDER_DESCENDING

public void sort(java.lang.String[] fieldNames, boolean[] sortAscending) 

Check the API reference Reference 1 or Reference 2

Sreehari
  • 5,621
  • 2
  • 25
  • 59