8

I am trying to sort a wrapper class list. But I am getting an error of "One or more of the items in this list is not Comparable". My sorting function is right and there is no issue but not understanding how this error coming. Any help is appreciated.

public Integer compareTo(Object compareTo)    
         {
                 importantWrapper appdata = (importantWrapper) compareTo; 
                 if (   this.duedateSorter  ==   appdata.duedateSorter   )
                 {
                    if( this.cardType  ==   appdata.cardType )
                    return 0;
                    else
                    {
                          if (  this.cardType   >   appdata.cardType  )
                          return 1;  
                          return -1;                     
                    }
                 }

                 if (  this.duedateSorter   >   appdata.duedateSorter  )
                  return 1;  
                  return -1; 
         } 
  • 1
    can you post some code mate ? – Mohith Shrivastava Dec 19 '13 at 13:53
  • Andrew gives a great answer, but @Mohitn Kumar is right, in the future, you need to post your code, and can't just say, 'the code is right'. 9 times out of 10, when someone thinks the code is not the issue, it is. – Chris Duncombe Dec 19 '13 at 14:46
  • I was focused on the sort method but real problem was beacuse the comparable interface was not implemented. My copy paste mistake, eaten up a couple of hours. – Shebin Mathew Dec 19 '13 at 15:06
8

This error occurs because one or more of them items in your list is not comparable (a key requirement for the sort feature). It is likely an instance of an Apex Class of your own or someone else's creation does not implement the Comparable interface. Or one of the Apex system classes that don't support it. Fortunatly you can implement this interface yourself and/or wrap the unsupported type.

Without being able to compare items being sorted the platform cannot perform the sorting for you and gives this error. Some but not all of the standard Apex classes support this, so you may not have encountered this until now. Take a look at the documentation for the sort method.

Using this method, you can sort primitive types, SelectOption elements, and sObjects (standard objects and custom objects). For more information on the sort order used for sObjects, see Sorting Lists of sObjects. You can also sort custom types (your Apex classes) if they implement the Comparable Interface interface.

This topic provides a good example of implementing this interface....

public class Employee implements Comparable {

    public Long id;
    public String name;
    public String phone;

    // Constructor
    public Employee(Long i, String n, String p) {
        id = i;
        name = n;
        phone = p;
    }

    // Implement the compareTo() method
    public Integer compareTo(Object compareTo) {
        Employee compareToEmp = (Employee)compareTo;
        if (id == compareToEmp.id) return 0;
        if (id > compareToEmp.id) return 1;
        return -1;        
    }
}

NOTE: global is not required, i've changed my copy of the code above to use public.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.