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
Flint LockwoodFlint Lockwood 

Custom Sort Wrapper Class

I created a wrapper class that implements the Comparable interface. However, when I sort the list of wrapper, the list is still sorting by the name
 
public with sharing class SampleSort implements Comparable{
	
	private Goal__c goalRecord;

	public CaseSLAWrapperClass(Goals__c s) {
              goalRecord = s;
	}

	public Integer compareTo(Object compareTo)
	{
		SampleSort compareToGoal = (SampleSort)compareTo;

		if(goalRecord.Display_Order__c < compareToGoal.goalRecord.Display_Order__c)
			return 1;
		else if (goalRecord.Display_Order__c < compareToGoal.goalRecord.Display_Order__c)
			return -1;
		else
			return 0;
	}
}

 
Alain CabonAlain Cabon
Hi,

 Instances of CaseSLAWrapperClass should be ordered perhaps?
 
public with sharing class CaseSLAWrapperClass implements Comparable{
	
	private Goal__c goalRecord;

	public CaseSLAWrapperClass(Goals__c s) {
              goalRecord = s;
	}

	public Integer compareTo(Object compareTo)
	{
        CaseSLAWrapperClass compareToGoal = (CaseSLAWrapperClass)compareTo;

        // The return value of 0 indicates that both elements are equal.
        Integer returnValue = 0;

	    if(goalRecord.Display_Order__c > compareToGoal.goalRecord.Display_Order__c)
		    return 1;
	    else if (goalRecord.Display_Order__c < compareToGoal.goalRecord.Display_Order__c)
		    return -1; 

        return returnValue;
	}
}

Regards

Alain
Deepak GulianDeepak Gulian

Please follow the below link for better understading of comparable interface.

http://www.infallibletechie.com/2013/08/comparable-interface-in-salesforce.html

Amit Chaudhary 8Amit Chaudhary 8
Apex class
global class Sample {     
    public List<Student> StudList {get;set;}   

    public Sample() {
        StudList = new List<Student>();
        Student s1 = new Student('Ganesh', 12);
        StudList.add(s1);
        
        Student s2 = new Student('Arun', 82);
        StudList.add(s2);
        
        Student s3 = new Student('Vignesh', 32);
        StudList.add(s3);
        
        Student s4 = new Student('Younis', 55);
        StudList.add(s4);     
        
        Student s5 = new Student('Azhar', 42);
        StudList.add(s3);   
        
        StudList.sort();
    }
    
    //wrapper class for Comparable Interface
    
    global class Student implements Comparable {
        global String StudentName {get;set;}    
        global Integer StudentAge {get; set;}
        
        global Student(String Name, Integer Age) {
            StudentName = Name;
            StudentAge = Age;
        }
        
        global Integer compareTo(Object ObjToCompare) {
            return StudentName.CompareTo(((Student)ObjToCompare).StudentName);
        }
    }
}
Page
<apex:page showHeader="false" controller="Sample" >
<apex:form >
    <apex:pageBlock >
        <apex:pageblockTable value="{!StudList}" var="S">
            <apex:column value="{!S.StudentName}"/>
            <apex:column value="{!S.StudentAge}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:form>     
</apex:page>

Check below post for more information
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_comparable.htm