(Salesforce Apex 2024 Release) A Deep Dive into List Element Comparison
You can now construct different sort orders in your code by
using List.sort() with a Comparator parameter, as the List class now implements
the new Comparator interface. For sorting and locale-sensitive comparisons, use
the new Collator class's getInstance function. due to the fact that
locale-sensitive sorting's outcomes can change depending on who runs the code.
utilizing it in code that anticipates a specific sort order
or in triggers, let's see with an example
public class Employee {
private Long id;
private String name;
//Constructor
public Employee(Long i, String n){
id=i;
name=n;
}
public String getName() { return name;}
}
Now, to add the sorting logic, we will create a class named NameCompare that implements the interface Comparator. We will add the body for the method compare of the Comparator interface, which will take two employees at a time and compare them by name using the help of the compareTo method. This process will continue until all the employees are compared two at a time, and it will return a sorted list of employees.
// Class to compare Employees by namepublic class NameCompare implements Comparator<Employee> { public Integer compare(Employee e1, Employee e2) { if(e1?.getName() == null && e2?.getName() == null) { return 0; } else if(e1?.getName() == null) { return -1; } else if(e2?.getName() == null) { return 1; } return e1.getName().compareTo(e2.getName()); } }
Now that the class blueprint is complete, we can utilize it
to sort our employees by name. Let's see how it goes.
List<Employee> empList = new List<Employee>();empList.add(new Employee(101,'Joe Smith', 2020)); empList.add(new Employee(102,'J. Smith', 2020)); empList.add(new Employee(25,'Caragh Smith', 1999)); empList.add(new Employee(105,'Mario Ruiz', 2019)); System.debug(empList); // Sort by name Employee.NameCompare nameCompare = new Employee.NameCompare(); empList.sort(nameCompare); System.debug(empList);
OUTPUT:
string userLocale = 'fr_FR';User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey=userLocale, TimeZoneSidKey='America/Los_Angeles', ProfileId = [SELECT Id FROM Profile WHERE Name='Standard User'].Id, UserName='standarduser' + DateTime.now().getTime() + '@testorg.com'); System.runAs(u) { List<String> shoppingList = new List<String> { 'épaule désosé Agneau', 'Juice', 'à la mélasse Galette 5 kg', 'Bread', 'Grocery' }; // Default sort shoppingList.sort(); Assert.areEqual('Bread', shoppingList[0]); // Sort based on user Locale Collator myCollator = Collator.getInstance(); shoppingList.sort(myCollator); Assert.areEqual('à la mélasse Galette 5 kg', shoppingList[0]); Assert.areEqual('Bread', shoppingList[1]); Assert.areEqual('épaule désosé Agneau', shoppingList[2]);
Now that we have list sort functionality exposed instead of just wrapper classes, things will get a lot easier.
Comments
Post a Comment