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;
                          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 ? Dec 19, 2013 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. Dec 19, 2013 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. Dec 19, 2013 at 15:06
  • 1
    Happened to me as well -- I copy pasted the compareTo method but forgot to copy paste the implements Comparable on the class definition
    – cropredy
    Oct 21, 2020 at 18:56

3 Answers 3


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.


I know its not the answer to this issue, but I ran into the same error and found out that "Comparable" need to be on the class implementation and not only on the Interface.

You can't do this:

public interface IEmployee extends Comparable {}

public class Employee implements IEmployee {
  public Integer compareTo(Object compareTo) {...}

But you need to do this:

public interface IEmployee {}

public class Employee implements IEmployee, Comparable {
  public Integer compareTo(Object compareTo) {...}

I have this error

One or more of the items in this list is not Comparable.

when I had actually all the items in the list implementing Comparable interface. However, in some cases the compare function wasn't returning 1, -1 or 0.

So we need implement the Comparable interface method in such a way that it always return at least 0.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .