Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Comparator Interface
Implement different sort orders with the Comparator interface’s compare() method, and pass the Comparator as a parameter to List.sort(). Your implementation must explicitly handle null inputs
in the compare() method to avoid a null pointer
exception.
Namespace
Comparator Methods
The following are methods for Comparator.
compare(var1, var2)
Compares the two arguments and returns a negative integer, zero, or a positive integer
depending on whether the first argument is less than, equal to, or greater than the second
argument.
Signature
public Integer compare(T var1, T var2)
Parameters
- var1: Type: T T - The type determined by the parameterized type of the Comparator. For example, if the class implements Comparator<Account> then var1 and var2 are of type Account .
- var2: Type: T T - The type determined by the parameterized type of the Comparator. For example, if the class implements Comparator<Account> then var1 and var2 are of type Account .
Return Value
Type: Integer
Comparator Example Implementation
Use the Comparator interface to impose different kinds of sorting.
This example implements two different ways of sorting employees.
1public class Employee {
2
3 private Long id;
4 private String name;
5 private Integer yearJoined;
6
7 // Constructor
8 public Employee(Long i, String n, Integer y) {
9 id = i;
10 name = n;
11 yearJoined = y;
12 }
13
14 public String getName() { return name; }
15 public Integer getYear() { return yearJoined; }
16}1// Class to compare Employees by name
2 public class NameCompare implements Comparator<Employee> {
3 public Integer compare(Employee e1, Employee e2) {
4 if(e1?.getName() == null && e2?.getName() == null) {
5 return 0;
6 } else if(e1?.getName() == null) {
7 return -1;
8 } else if(e2?.getName() == null) {
9 return 1;
10 }
11 return e1.getName().compareTo(e2.getName());
12 }
13 }
14
15 // Class to compare Employees by year joined
16 public class YearCompare implements Comparator<Employee> {
17 public Integer compare(Employee e1, Employee e2) {
18 // Guard against null operands for ‘<’ or ‘>’ operators because
19 // they will always return false and produce inconsistent sorting
20 Integer result;
21 if(e1?.getYear() == null && e2?.getYear() == null) {
22 result = 0;
23 } else if(e1?.getYear() == null) {
24 result = -1;
25 } else if(e2?.getYear() == null) {
26 result = 1;
27 } else if (e1.getYear() < e2.getYear()) {
28 result = -1;
29 } else if (e1.getYear() > e2.getYear()) {
30 result = 1;
31 } else {
32 result = 0;
33 }
34 return result;
35 }
36 }
37The following example tests the implementation:
1@isTest
2private class EmployeeSortingTest {
3 @isTest
4 static void sortWithComparators() {
5 List<Employee> empList = new List<Employee>();
6 empList.add(new Employee(101,'Joe Smith', 2020));
7 empList.add(new Employee(102,'J. Smith', 2020));
8 empList.add(new Employee(25,'Caragh Smith', 2021));
9 empList.add(new Employee(105,'Mario Ruiz', 2019));
10 // Sort by name
11 NameCompare nameCompare = new NameCompare();
12 empList.sort(nameCompare);
13 // Expected order: Caragh Smith, J. Smith, Joe Smith, Mario Ruiz
14 Assert.areEqual('Caragh Smith', empList.get(0).getName());
15
16 // Sort by year joined
17 YearCompare yearCompare = new YearCompare();
18 empList.sort(yearCompare);
19 // Expected order: Mario Ruiz, J. Smith, Joe Smith, Caragh Smith
20 Assert.areEqual('Mario Ruiz', empList.get(0).getName());
21 }
22}