Sorting of Primitive Data Types like <String> (eg. List of student names) is easy in collection.
But suppose you have a scenario like below,
So, for the above you can not use simple sorting mechanism. Here is the concept of wrapper class sorting.
For the same Salesforce provides us the " Comparable" INTERFACE.
Lets check the sample apex code for the above scenario.
########### Create an Employee Wrapper Class ###########
########### Create an EmployeeComparatorEg Class which will implements Comparable Interface and hence define it's compareTo() method###########
########### Finally create a client class to test the sorting scenario ###########
########### From Anonymous Window call getListOfEmpNameAsSortedOrder() method of above EmployeeComparatorTestEg class to check
the normal and sorted list of employee in debug log.###########
Execute the below line of code in Anonymous window.
EmployeeComparatorTestEg.getListOfEmpNameAsSortedOrder();
Output:
15:32:48:013 USER_DEBUG [10]|DEBUG|Simple list of employee:::::::::::::::
(EmployeeWrapperEg:[empId=E-100, empName=Mark, empSalary=10000.0],
EmployeeWrapperEg:[empId=E-102, empName=Jon, empSalary=9000.0],
EmployeeWrapperEg:[empId=E-101, empName=Luce, empSalary=9500.0])
15:32:48:014 USER_DEBUG [18]|DEBUG|Sorted list by name :::::::::::::::
(EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-102, empName=Jon, empSalary=9000.0]],
EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-101, empName=Luce, empSalary=9500.0]],
EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-100, empName=Mark, empSalary=10000.0]])
But suppose you have a scenario like below,
- You have a list of employees (Wrapper Class).
- Say this Employee wrapper contains few primitive type like <employee name, employee id, employee salary etc.)
- Now the problem is that we have to sort the list of Employees based on their name.
So, for the above you can not use simple sorting mechanism. Here is the concept of wrapper class sorting.
For the same Salesforce provides us the " Comparable" INTERFACE.
Lets check the sample apex code for the above scenario.
########### Create an Employee Wrapper Class ###########
public class EmployeeWrapperEg{ public String empName; public String empId; public Double empSalary; }
public class EmployeeComparatorEg implements Comparable{ public EmployeeWrapperEg emplInst; // Constructor public EmployeeComparatorEg(EmployeeWrapperEg emp) { emplInst = emp; } public Integer compareTo(Object compareTo){ // Cast argument to EmployeeWrapper EmployeeComparatorEg compareToEmpl = (EmployeeComparatorEg)compareTo; // The return value of 0 indicates that both elements are equal. Integer returnValue = 0; if(emplInst.empName > compareToEmpl.emplInst.empName){ // Set return value to a positive value. returnValue = 1; } else if(emplInst.empName < compareToEmpl.emplInst.empName){ // Set return value to a negative value. returnValue = -1; } return returnValue; } }
public class EmployeeComparatorTestEg{ /** Client method to get sorted list of employees based on their * Name with the help of Salesforce's Comparable Interface. */ public Static List<EmployeeComparatorEg> getListOfEmpNameAsSortedOrder(){ List<EmployeeWrapperEg> lstOfEmps = new List<EmployeeWrapperEg>(); lstOfEmps = getListOfEmployee(); System.debug('Simple list of employee::::::::::::::: '+lstOfEmps); List<EmployeeComparatorEg> lstOfSortedEmps = new List<EmployeeComparatorEg>(); for(EmployeeWrapperEg empIns : lstOfEmps){ lstOfSortedEmps.add(new EmployeeComparatorEg(empIns)); } lstOfSortedEmps.sort(); System.debug('Sorted list by name ::::::::::::::: '+lstOfSortedEmps); return lstOfSortedEmps; } /** Initialize list of employee wrapper. For simplicity I have make it manually. * It can be set from different source like database or any external system. */ private static List<EmployeeWrapperEg> getListOfEmployee(){ List<EmployeeWrapperEg> lstOfEmps = new List<EmployeeWrapperEg>(); EmployeeWrapperEg epm1 = new EmployeeWrapperEg(); epm1.empName = 'Mark'; epm1.empId = 'E-100'; epm1.empSalary = 10000; EmployeeWrapperEg epm2 = new EmployeeWrapperEg(); epm2.empName = 'Jon'; epm2.empId = 'E-102'; epm2.empSalary = 9000; EmployeeWrapperEg epm3 = new EmployeeWrapperEg(); epm3.empName = 'Luce'; epm3.empId = 'E-101'; epm3.empSalary = 9500; lstOfEmps.add(epm1); lstOfEmps.add(epm2); lstOfEmps.add(epm3); return lstOfEmps; } }
the normal and sorted list of employee in debug log.###########
Execute the below line of code in Anonymous window.
EmployeeComparatorTestEg.getListOfEmpNameAsSortedOrder();
Output:
15:32:48:013 USER_DEBUG [10]|DEBUG|Simple list of employee:::::::::::::::
(EmployeeWrapperEg:[empId=E-100, empName=Mark, empSalary=10000.0],
EmployeeWrapperEg:[empId=E-102, empName=Jon, empSalary=9000.0],
EmployeeWrapperEg:[empId=E-101, empName=Luce, empSalary=9500.0])
15:32:48:014 USER_DEBUG [18]|DEBUG|Sorted list by name :::::::::::::::
(EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-102, empName=Jon, empSalary=9000.0]],
EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-101, empName=Luce, empSalary=9500.0]],
EmployeeComparatorEg:[emplInst=EmployeeWrapperEg:[empId=E-100, empName=Mark, empSalary=10000.0]])