1

I have a wrapper class which is sorted by the totalCount of the size of the three list (empSkillList, empTrainingList, and empCertificationList). Can I sort it also by the fields inside the list of the custom object? If yes, how to do so? Or will I convert it first to a list of string so I can compare it?

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;}

    //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 0;
        }
    }

}

How can I access the fields in the list and compare each record inside the list?

Integer compareFields(pageFilter that){ 
    if(this.empSkillList.Skill_ID__r.Name > that.empSkillList.Skill_ID__r.Name){ 
        return 1; } 
//so on and so forth 
} 

This is the sample code that we are analyzing but since we cannot access the certain record in the list, this code will not work.

2

Yes, you can add fields to compare when the child count is the same:

public Integer compareTo(Object instance)
{
    PageFilter that = (PageFilter)instance;
    if (that.totalCount == this.totalCount)
    {
        return compareFields(that);
    }
    return that.totalCount - this.totalCount;
    // or simply:
    // return (that.totalCount == this.totalCount) ?
    //    compareFields(that) : that.totalCount - this.totalCount;
}
Integer compareFields(PageFilter that)
{
    // sort on text fields instead
}

Note also that you can greatly simplify the total count:

public Integer totalCount
{
    get
    {
        Integer skillCount = (empSkillList == null) ? 0 : empSkillList.size();
        Integer trainingCount = (empTrainingList == null) ? 0 : empTrainingList.size();
        Integer certificationCount = (empCertificationList == null) ? 0 : empCertificationList.size();
        return skillCount + trainingCount + certificationCount;
    }
}

Much less complicated to test and maintain.

  • See updated problem @AdrianLarson :) – JMRAF Apr 10 '17 at 2:14
  • @JMRAF You do realize there are multiple skills? Are you trying to sort on the lowest alphabetically? – Adrian Larson Apr 10 '17 at 13:37

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.