0

I am attempting to sort a list a list in my class however I am running into the error above. The list comes from a method in another class.

public static List< vCtrl.CheckboxOption> convertPicklistValuesIntoCheckBox( Map<String, SobjectField> fieldMap,String fieldName){

        Schema.DescribeFieldResult legacy = fieldMap.get(fieldName).getDescribe();
        List<Schema.PicklistEntry> legacySeries= legacy.getPicklistValues();
        List< vCtrl.CheckboxOption> listOfCheckBoxOption = new List< vCtrl.CheckboxOption>();
        for (Schema.PicklistEntry entry : legacySeries) 
        {
            vCtrl.CheckboxOption boxOption = new  vCtrl.CheckboxOption();
            boxOption.label = entry.getLabel();
            boxOption.value = entry.getValue();
            boxOption.selected = false;
            listOfCheckBoxOption.add(boxOption);
        }    

        return listOfCheckBoxOption;
    }

I am using the list below in class vCtrl and want it sorted alphabetically. Calling List.sort() method on this list throws error: One or more of the items in this list is not Comparable.

listMake  =  Utill.convertPicklistValuesIntoCheckBox(fieldMap,'Make__c');
listMake.sort(); 
2
  • What are the list values?
    – Jagular
    Commented Jul 10, 2014 at 14:05
  • Can you show us the class vCtrl.CheckboxOption ?
    – FrankZ
    Commented Jul 10, 2014 at 14:12

2 Answers 2

1

Not knowing how your CheckboxOption class looks like I try to answer using some assumptions:

Let's assume your class looks like this:

public class CheckboxOption {
    public Boolean selected { get; set; }
    public String label { get; set; }
    public String value { get; set; }
}

You need to make it implement the interface Comparable:

public class CheckboxOption implements Comparable {
    public Boolean selected { get; set; }
    public String label { get; set; }
    public String value { get; set; }

    public Integer compareTo(Object compareTo) {
        CheckboxOption other = (CheckboxOption)compareTo;
        return this.label.compareTo(other.label);
}

Haven't tested this snippet, but you should get the idea.

0

This error occurs because one or more of them items in your list is not comparable. To make comparison work the objects in list must implement Comparable interface.

Only primitives, SelectOption, and sObjects can be cmpared and here you are doing something with Schema I guess which isn't supported.

You must log in to answer this question.

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