-1

I want to sort a custom wrapper list. A piece of code is shown below.

public List<Subscription__c> subs;  
public TerritoryBusinessUnit__c tlBU;

public class wrap {

        public boolean selection { set; get; }
        public subscription__c sub { set; get; }

        public wrap(boolean sel, subscription__c s) {

            selection = sel;
            sub = s;
        }
    }

public List<wrap> wrapls = new List<wrap>();

    public List<wrap> subList { 
        set; 
        get{
            return wrapls;
        } 
    }

public void Laodsubscriptions()
{
  subs=[some condition here]

tlBU=[SELECT ID,Name, TerritoryLabel__c, MID__c, FetchType__c,Address__c
                    FROM TerritoryBusinessUnit__c
                    WHERE MID__c=:ApexPages.currentpage().getparameters().get('mid')];

if(tlBu.FetchType__c == 'L')
{
  //some logic
}

wraps.sort();
}

Error:

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

Please let me know what best can be done to resolve this.

1 Answer 1

1

This interface is covered in the Apex Developer Guide:

Comparable Interface

Adds sorting support for Lists that contain non-primitive types, that is, Lists of user-defined types.

global 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
    global Integer compareTo(Object compareTo) {
        Employee compareToEmp = (Employee)compareTo;
        if (id == compareToEmp.id) return 0;
        if (id > compareToEmp.id) return 1;
        return -1;        
    }
}

Please, never use the global access modifier unless you know of a solid reason you need it. Replace global with public when rolling your own implementation.

You must log in to answer this question.

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