+ Start a Discussion
dlCamelotdlCamelot 

Merge 2 Lists & Keep 1 Set Prioritized Over the Other

How do I merge these two lists of results into a master list (ex: dnbFinalMatchList )?  What would also be nice to have is that the list order has dnbCompleteMatches before dnbPartialMatches.  

Class So Far:
public static List<String> getDnBMatches(String companyName){
    	List<String> dnbFinalMatchList = new List<String>();  
        List<String> dnbCompleteMatches = new List<String>(); 
        List<String> dnbPartialMatches = new List<String>();
    	List<String> nameFragments = new List<String>();
        Integer emptyList = 0;
        Map<id,DatacloudDandBcompany> dnbData = new Map<id,DatacloudDandBcompany>();
   
        if (String.isBlank(companyName)){
            	return null;
        }else{
            	for (String fragment : companyName.split(' ')){
                     nameFragments.add('%' + fragment + '%');
                     }
                dnbData.putall([SELECT companyId FROM DatacloudDandBCompany WHERE Name LIKE :nameFragments]);
                for (DatacloudDandBCompany c: dnbData.values()){
                    if (companyName == c.name){
                        dnbCompleteMatches.add(c.companyid);
                    }
                    dnbPartialMatches.add(c.companyid); 
                }

                if (dnbCompleteMatches.size() > emptyList) {
                    return dnbCompleteMatches;
                }
                else {
                    return dnbPartialMatches;
                }
         
        }
    }

 
Maharajan CMaharajan C
Hi dlCamelot,

We have to use addAll function:

dnbFinalMatchList.addAll(dnbCompleteMatches);
dnbFinalMatchList.addAll(dnbPartialMatches);
return dnbFinalMatchList;

Can you please Let me know if it works or not and also If you face any problems!!!

If it works don't forget to mark this as a best answer!!!

Thanks,
​Raj
dlCamelotdlCamelot
Is this what you meant?
public static List<String> getDnBMatches(String companyName){
    	List<String> dnbFinalMatchList = new List<String>();  
        List<String> dnbCompleteMatches = new List<String>(); 
        List<String> dnbPartialMatches = new List<String>();
    	List<String> nameFragments = new List<String>();
        Integer emptyList = 0;
        Map<id,DatacloudDandBcompany> dnbData = new Map<id,DatacloudDandBcompany>();
   
        if (String.isBlank(companyName)){
            	return null;
        }else{
            	for (String fragment : companyName.split(' ')){
                     nameFragments.add('%' + fragment + '%');
                     }
                dnbData.putall([SELECT companyId FROM DatacloudDandBCompany WHERE Name LIKE :nameFragments]);
                for (DatacloudDandBCompany c: dnbData.values()){
                    if (companyName == c.name){
                        dnbCompleteMatches.add(c.companyid);
                    }
                    dnbPartialMatches.add(c.companyid); 
                }

                if (dnbCompleteMatches.size() > emptyList ) {
                    dnbFinalMatchList.addAll(dnbCompleteMatches); 
                }
               if(dnbPartialMatches.size() emptyList) {
                    dnbFinalMatchList.addall(dnbPartialMatches);
                  }
                    return dnbFinalMatchList;
                }
         
        }
    }