この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Comparator インターフェース

Comparator インターフェースの compare() メソッドを使用してさまざまな並べ替え順を実装し、Comparator をパラメーターとして List.sort() に渡します。実装では、null ポインター例外を回避するために、compare() メソッドで明示的に null 入力を処理する必要があります。

名前空間

System

Comparator のメソッド

Comparator のメソッドは次のとおりです。

compare(var1, var2)

2 つの引数を比較し、先頭の引数が 2 番目の引数より小さいか、等しいか、大きいかに応じて、負の整数、ゼロ、正の整数を返します。

署名

public Integer compare(T var1, T var2)

パラメーター

var1
型: T
T - Comparator のパラメーター化された型で決定される型。たとえば、クラスが Comparator<Account> を実装している場合、var1var2 の型は Account です。
var2
型: T
T - Comparator のパラメーター化された型で決定される型。たとえば、クラスが Comparator<Account> を実装している場合、var1var2 の型は Account です。

戻り値

型: Integer

Comparator の実装例

Comparator インターフェースは、さまざまな種類の並べ替えを適用するために使用します。

次の例では、従業員を並べ替える 2 種類の方法を実装しています。

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

次の例では、実装をテストしています。

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}