Thursday, May 4, 2017

How to Sort Wrapper class Collection in Apex

Sorting of Primitive Data Types like <String> (eg. List of student names) is easy in collection.
But suppose you have a scenario like below,

  1. You have a list of employees (Wrapper Class).
  2. Say this Employee wrapper contains few primitive type like <employee name, employee id, employee salary etc.)
  3. 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;
}
########### Create an EmployeeComparatorEg Class which will implements Comparable Interface and hence define it's compareTo() method###########




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;
    }
}
########### Finally create a client class to test the sorting  scenario ###########





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;
    }
}
########### 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]])

LWC to LWC Communication using Lightning Messaging Service (Part - 2)

In my previous post ( previous post link ) we have learn how to create a Lightning Messaging Service with 6 steps and today we will use the ...