2

I have 3 lookup relationship with contact and I created a List for each lookup. For every list, I put it on a Set of IDs which is being used in the contact list in order to related the lookup on the contact. I put everything on a wrapper class so that every skill, training or cert will check if the id is matched on the contact Id.

public class pageFilter {

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

pageFilterList = new List<pageFilter>();

for(Contact cons : contList ){
        pgf = new pageFilter();                   
        pgf.employee = cons;

        // SKILLS
        if(!String.isEmpty(wrapFilter1.skillName1) || !String.isEmpty(wrapFilter2.skillName2) || !String.isEmpty(wrapFilter3.skillName3) || !String.isEmpty(wrapFilter4.skillName4) || !String.isEmpty(wrapFilter5.skillName5) ) {  

            List<Employee_Skills__c> empSkills = new List<Employee_Skills__c>();

            for(Employee_Skills__c empSkill : skillList ){
                if(cons.Id == empSkill.Employee_Id__c){
                    empSkills.add(empSkill);                       
                } 
            }
            pgf.empSkillList = empSkills;
        }
        pageFilterList.add(pgf);
}

We have 5 filters, each of the filters can search for skill, training or certification.

How to sort the Contact ASC and group the search result by the most number of matched result.

  • If you want to sort by skills, training or certification then leverage Comparable interface – Santanu Boral Apr 3 '17 at 1:10
  • Hi Santaru, we used order by on the skill, training and cert in order to sort based on the requirement. Our problem now is, how we will sort the contact list for example the contact matched only (1) out of the number of filters used. – lone Apr 3 '17 at 1:25
  • Hi Santaru, result should be like this. Click this sample result – lone Apr 3 '17 at 1:43
  • I would still suggest to use Comparable interface for custom sorting. and to display the data with rowspan, refer my blog: santanuboral.blogspot.com/2017/02/… – Santanu Boral Apr 3 '17 at 1:57
1

As Santanu commented, if you implement the Comparable interface on pageFilter then you can use Apex to sort by whatever criteria you want.

Here I only check to total count of all skills, training and certifications.

public 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 integer totalCount {
        get {
            // Might need to check for nulls here...
            return empSkillList.size() + empTrainingList.size() + empCertificationList.size();
        }
    }             

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

    }
}

Then you just need to apply the sorting:

pageFilterList.sort();

In most cases where I've implemented Comparable I'll have a SortingBy enum and SortAsc boolean that I can use to control what controls the sort ordering and if the direction should be ascending or descending.

| improve this answer | |
  • 1
    actually (something I learned from sfdcfox), you don't need -1 and +1, just values >0 and < 0 so the compareTo method's if statement can be reduced to : `return this.totalCount - compareToFilter.totalCount; – cropredy Apr 3 '17 at 5:15
  • @cropredy Yes, that would certain be a good optimization if you only wanted to compare two integers. I kept the example in the expanded out form as it can make it clearer where you could do additional comparisons for secondary sorting, etc... – Daniel Ballinger Apr 3 '17 at 7:51
  • @DanielBallinger is it possible that in the table result i will first prioritize the matched filtered in skills before I add it to empTrainingList.size() and empCertificationList.size() ?? Please check my question here please. Please check.... – lone Apr 11 '17 at 5:36

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.