function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Will EdwardsWill Edwards 

How does one combine two lists, merge them, remove duplicates, and then sort in Apex?

In an Apex class, I'd like to take two lists of contacts, merge them, remove duplicate contacts, and then sort them? Any suggestions? Thanks.
 
Best Answer chosen by Will Edwards
Apoorv Saxena 4Apoorv Saxena 4
Hi Will,

Please try the code provided below :
 
List<Contact> conList1=[SELECT Id, Name FROM Contact ORDER BY Name limit 10];

List<Contact> conList2=[SELECT Id, Name FROM Contact ORDER BY Name limit 5];
List<Contact> finalConList;
conList1.addAll(conList2);

conList1.sort();


Set<Id> conSet= new Set<Id>();
for(Contact con:conList1){
    conSet.add(con.id);
}

finalConList = [Select id,name from Contact where id in:conSet order by name];
system.debug('Your final sorted list without duplicate'+finalConList);

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv

All Answers

badibadi
Yes you can use a combination of List and Set methods to do that.
 
List<Contact> contacts=[SELECT Id, Name, AccountId FROM Contact ORDER BY CreatedDate DESC LIMIT 20];
System.debug(contacts);
System.debug(contacts.size());

List<Contact> contacts1=[SELECT Id, Name, AccountId FROM Contact ORDER BY CreatedDate ASC LIMIT 10];
System.debug(contacts1);
System.debug(contacts1.size());

contacts.addAll(contacts1);
System.debug(contacts);
System.debug(contacts.size());

contacts.sort();
System.debug(contacts);
System.debug(contacts.size());

Set<Contact> allCons= new Set<Contact>();
allCons.addAll(contacts);
System.debug(allCons);
System.debug(allCons.size());

Hope this helps
Will EdwardsWill Edwards
Badi, how do I end up with a list again? Also, how do I choose which field to sort over? Say, for example, that I want to sort by name? Thanks!!
badibadi
Will, 
List<Contacts> finalCons= new List<Contact>();
finalCons.addAll(allCons);

adding the above code at the end of previous block will give you a list again.

The example here sorts contacts by name. You can sort by other fields as well by implementing comparable interface. You can find more infornation in this link https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_list_sorting_sobject.htm

Thanks
Apoorv Saxena 4Apoorv Saxena 4
Hi Will,

Please try the code provided below :
 
List<Contact> conList1=[SELECT Id, Name FROM Contact ORDER BY Name limit 10];

List<Contact> conList2=[SELECT Id, Name FROM Contact ORDER BY Name limit 5];
List<Contact> finalConList;
conList1.addAll(conList2);

conList1.sort();


Set<Id> conSet= new Set<Id>();
for(Contact con:conList1){
    conSet.add(con.id);
}

finalConList = [Select id,name from Contact where id in:conSet order by name];
system.debug('Your final sorted list without duplicate'+finalConList);

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
This was selected as the best answer
Will EdwardsWill Edwards
Apoorv, that's great. Thanks!!!