function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shivram survaseshivram survase 

How can i sort the wrapper list on more than 1 field?

Here is my code.. and It works perfectly for sorting on Name field.

I want to sort on Name field,If Names are same ,Sorting should be on Email field.

 global class cContact implements Comparable{
        public Contact cnt {get; set;}
        public Boolean cntSelected {get; set;}
        public cContact(Contact c) {
            cnt = c;
            cntSelected = false;
        }
        global Integer compareTo(Object compareTo) {
            cContact compareToCnt = (cContact)compareTo;
            String s1=cnt.Name;
            String s2=compareToCnt.cnt.Name;
            return (s1.compareTo(s2));       
        }
    }

Help is appreciated in advance.
bob_buzzardbob_buzzard
Just check if the names are the same and if they are, switch the compareto to work on the email field:
 
global Integer compareTo(Object compareTo) {
        Integer result;

        cContact compareToCnt = (cContact)compareTo;
        String s1=cnt.Name;
        String s2=compareToCnt.Name;
        if (s1==s2)
        {
           String e1=cnt.Email;
           String e2=compareToCnt.Email;
           result=e1.compareTo(e2);
        }
        else
        {
            result=(s1.compareTo(s2));       
        }

        return result;
}