Skip to main content
added 421 characters in body
Source Link
JMRAF
  • 31
  • 4

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.

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.

deleted 11 characters in body
Source Link
Adrian Larson
  • 151.3k
  • 38
  • 247
  • 431

Thanks!

Thanks!

Source Link
JMRAF
  • 31
  • 4

Sorting of fields of custom object using comparable in a wrapper

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

Thanks!