Comparator
The Comparator
interface is used to order objects of an arbitrary class. It is not to be confused with the Comparable
interface, which is implemented by the class to be sorted. The Comparator
interface is implemented in a separate class.
Syntax
class MyComparator implements Comparator<MyClass> {
@Override public int compare(MyClass a, MyClass b)
{
// Compare logic
...
return result;
}
}
Applying the Comparator
interface to a class, MyComparator
, requires the implements
keyword (e.g., Comparator<MyClass>
). This interface has a .compare()
method that returns an int
value based on whether two MyClass
instances, a
and b
, can be logically sorted.
Return Value | Meaning |
---|---|
>= 1 |
first object instance > second object instance |
0 |
first object instance = second object instance |
<= -1 |
first object instance < second object instance |
A Comparator
class can be passed as an argument to methods such as Arrays.sort()
and Collections.sort()
to specify the sort order, potentially overriding the natural sort order defined by the class’s own .compareTo()
method.
Example
The following example showcases the Comparator
interface. First, an Employee
class is defined:
// Employee.javapublic class Employee {String firstName;String lastName;// Constructor sets firstName and lastNamepublic Employee(String first, String last){this.firstName = first;this.lastName = last;}// User-friendly output when printed.public String toString(){return "( " + lastName + ", " + firstName + " )";}}
The next snippet defines an EmployeeSort
class that implements the Comparator
interface and overrides its .compare()
method to sort based on lastName
and then on firstName
:
import java.util.*;// EmployeeSort.javapublic class EmployeeSort implements Comparator<Employee> {// Implement the Comparator interface@Override public int compare(Employee valueA, Employee valueB){if (valueA.lastName.compareTo(valueB.lastName) != 0) {// If lastNames are different, compare lastNamereturn valueA.lastName.compareTo(valueB.lastName);} else {// If lastNames are the same, compare firstNamereturn valueA.firstName.compareTo(valueB.firstName);}}}
This last snippet demonstrates the Comparator
interface:
import java.util.*;// SortExample.javapublic class SortExample {public static void main(String[] args){// Set up array with a few Employee classesEmployee a[] = new Employee[5];a[0] = new Employee("Kirk","Douglas");a[1] = new Employee("Mel","Brooks");a[2] = new Employee("Jane","Fonda");a[3] = new Employee("Henry","Fonda");a[4] = new Employee("Michael","Douglas");// Use .sort() method with Comparable class.Arrays.sort(a, new EmployeeSort());// Print out the sorted Employeesfor (int i=0; i < a.length; i++) {System.out.println(a[i]);}}}
This example results in the following output:
( Brooks, Mel )( Douglas, Kirk )( Douglas, Michael )( Fonda, Henry )( Fonda, Jane )
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.