0

We have a wrapper class which contains different list of custom object. We want to sort it through the field referenced by different custom object. How can we achieve this?

The code below shows a null pointer execution.

    global class pageFilter implements comparable {

    public Contact employee{get;set;}
    public List<Employee_Skills__c> empSkillList{get;set;} 
    public List<Employee_Training__c> empTrainingList{get;set;}
    public List<Employee_Certification__c> empCertificationList{get;set;}
    public pageFilterForEmployeeSkills empSkillCompare{get;set;}

    //related list of skill, training and Cert
    public List<Employee_Skills__c> relatedSkillList{get;set;}
    public List<Employee_Training__c> relatedTrainingList{get;set;}
    public List<Employee_Certification__c> relatedCertificationList{get;set;} 



    public integer totalCount { 
       get {
            // Grouped based on the number of matched filter
            if (empSkillList != null && empTrainingList == null && empCertificationList == null) {                    
                return empSkillList.size();    
            }

            if (empTrainingList != null && empSkillList == null && empCertificationList == null) {
                return empTrainingList.size();    
            }                 

            if (empCertificationList != null && empSkillList == null && empTrainingList == null) {
                return empCertificationList.size();
            }

            if (empSkillList != null && empTrainingList != null && empCertificationList == null ) {
                return empSkillList.size() + empTrainingList.size();
            }

            if (empSkillList != null && empTrainingList == null && empCertificationList != null ) {
                return empSkillList.size() + empCertificationList.size();
            }                        

            if (empSkillList == null && empTrainingList != null && empCertificationList != null) {
                return empTrainingList.size() + empCertificationList.size();
            }

            if (empSkillList != null && empTrainingList != null && empCertificationList != null ) {
                return empSkillList.size() + empTrainingList.size() + empCertificationList.size();                   
            }
            return  0; 

        }                      
    } 

    public Integer compareTo(Object compareTo) {
    pageFilter compareToFilter = (pageFilter)compareTo;

        if(this.totalCount < compareToFilter.totalCount) {
            return 1;
        } else if(this.totalCount > compareToFilter.totalCount) {
            return -1;
        } else {
            // You could check further criteria when counts are the same.
            return this.empSkillCompare.compareTo(compareToFilter.empSkillCompare);
        }
    }

}

global class pageFilterForEmployeeSkills implements comparable {

    public Contact employee{get;set;}
    public List<Employee_Skills__c> empSkillList{get;set;}
    public List<Employee_Training__c> empTrainingList{get;set;}
    public List<Employee_Certification__c> empCertificationList{get;set;}

    public Integer compareTo(Object o) {            
        pageFilterForEmployeeSkills thatSkill = (pageFilterForEmployeeSkills)o;
        if (this.empSkillList[0].Name < thatSkill.empSkillList[0].Name) return -1;
        else if (this.empSkillList[0].Name > thatSkill.empSkillList[0].Name) return 1;
        else return 0;

    } 

}

Thanks!

  • Can cannot sort list of sObject with comparable interface, you need to use a wrapper class to store List<Employee_Skills__c> and write the comparable interface in its own wrapper. Or best would be to do it in SOQL itself – Raul Apr 7 '17 at 7:16
  • Hi @RahulSharma, we use a wrapper class for List<Employee_Skills__c>, it's indicated by the wrapper class pageFilterForEmployeeSkills, and we create that wrapper to implement sorting, but the problem is we can't sort due to NPE. – boy george Apr 7 '17 at 7:34
  • referencing this site. salesforce.stackexchange.com/questions/77232/… – boy george Apr 7 '17 at 7:38
  • I think the problem is at empSkillList[0].Name, change the data type for empSkillList to List<Wrapper> – Raul Apr 7 '17 at 8:07
  • 1
    As your class is structured now, you will need to convert the list of SObjects into a list of wrapped SObjects, sort that list, then convert back to unwrapped SObjects. – Keith C Apr 7 '17 at 8:30
0

In answer to providing an example of wrapping, sorting and unwrapping, here is the sort of code that is needed:

private class Esw implements Comparable {
    Employee_Skills__c sob;
    Esw(Employee_Skills__c sob) { 
        this.sob = sob;
    }
    public Integer compareTo(Object o) {            
        Esw that = (Esw) o;
        if (this.sob.Name < that.sob.Name) return -1;
        else if (this.sob.Name > that.sob.Name) return 1;
        else return 0;
    } 
}

public List<Employee_Skills__c> sortedEmployeeSkills() {

     // Wrap and sort
     List<Esw> ws = new List<Esw>();
     for (Employee_Skills__c sob : empSkillList) ws.add(new Esw(sob));
     ws.sort();

     // Pull SObjects back out
     List<Employee_Skills__c> sorteds = new List<Employee_Skills__c>();
     for (Esw w : ws) sorteds.add(w.sob);
     return sorteds;
}

In some cases you are better off building the list of wrapped objects once only and manipulating those lists across your application to avoid this rather painful process of wrapping and unwrapping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.