Java Comparators are used to sort the user defined objects. It gives you flexibility to sort the objects in multiple ways. For example you can sort strings by alphabetical order, or by their lengths. If you have a People class with first name, last name and age, you can sort by the name, age or combinations. The syntax of custom comparator in Java are confusing. Applying the examples provided here as template will save you time.
1. Define Java comparator as class
After you define the custom class, you define Comparator as a separate class. The class implements Comparator interface. Inside the class, implement compare() method to compare two objects, and return an integer. Since it is class, you have to new class which will be the input parameter for the sort() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import java.util.*; class Country { protected int id; protected String name; //Constructor, Time O(1), Space O(1) public Country(int id, String name) { this.id = id; this.name = name; } //Override, Time O(1), Space O(1) public String toString() { return "(" + id + " " + name + ")"; } } class OrderByIdComparator implements Comparator<Country> { //Time O(1), Space O(1) @Override public int compare(Country c1, Country c2) { return c1.id - c2.id ; } } class OrderByNameComparator implements Comparator<Country> { //Time O(1), Space O(1) @Override public int compare(Country c1, Country c2) { return c1.name.compareTo(c2.name); } } class OrderByTwoComparator implements Comparator<Country> { //Time O(1), Space O(1) @Override public int compare(Country c1, Country c2) { if (c1.id != c2.id) return c1.id - c2.id; else return c1.name.compareTo(c2.name); } } public class DefineComparatorAsClass { public static void main(String[] args) { //List use cases List<Country> countries = new ArrayList<>(); countries.add(new Country(3,"Spain")); countries.add(new Country(1,"Kenya")); countries.add(new Country(4,"Saudi arabia")); countries.add(new Country(2,"Mexico")); countries.add(new Country(2,"America")); //Time O(nlogn) Space O(n), n is number of countries countries.sort(new OrderByIdComparator()); System.out.println("Sort countries by Id: "); System.out.println(countries); //Time O(nlogn) Space O(n), n is number of countries countries.sort( new OrderByNameComparator()); System.out.println("\nSort countries by name: "); System.out.println(countries); //Time O(nlogn) Space O(n), n is number of countries countries.sort(new OrderByTwoComparator()); System.out.println("\nSort countries by id and name"); System.out.println(countries); } } |
2. Define comparator as static method
This is a variation of defining comparator in Java. You declare a static method of Comparator and call new Comparator(), followed by the implementation of compare() method. Then you can sort with the method as the input parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
public class DefineComparatorAsStaticVariable { static class Country { protected int id; protected String name; //Constructor, Time O(1), Space O(1) public Country(int id, String name) { this.id = id; this.name = name; } //Override, Time O(1), Space O(1) public String toString() { return "(" + id + " " + name + ")"; } } //Sort by country id static Comparator<Country> ORDER_BY_ID = new Comparator<Country>() { public int compare(Country c1, Country c2) { return c1.id - c2.id ; } }; //Sort by country name static Comparator<Country> ORDER_BY_NAME = new Comparator<Country>() { public int compare(Country c1, Country c2) { return c1.name.compareTo(c2.name); } }; //Sort by country id and name static Comparator<Country> ORDER_BY_TWO = new Comparator<Country>() { public int compare(Country c1, Country c2) { if (c1.id != c2.id) return c1.id - c2.id; else return c1.name.compareTo(c2.name); } }; public static void main(String args[]) { List<Country> countries = new ArrayList<>(); countries.add(new Country(3,"Spain")); countries.add(new Country(1,"Kenya")); countries.add(new Country(4,"Saudi arabia")); countries.add(new Country(2,"Mexico")); countries.add(new Country(2,"America")); //Time O(nlogn) Space O(n), n is number of countries countries.sort(ORDER_BY_ID); System.out.println("Sort countries by Id: "); System.out.println(countries); //Time O(nlogn) Space O(n), n is number of countries countries.sort(ORDER_BY_NAME); System.out.println("\nSort countries by name: "); System.out.println(countries); //Time O(nlogn) Space O(n), n is number of countries countries.sort(ORDER_BY_TWO ); System.out.println("\nSort countries by id and name: "); System.out.println(countries ); } } |
Output:
Sort countries by Id:
[(1 Kenya), (2 Mexico), (2 America), (3 Spain), (4 Saudi arabia)]
Sort countries by name:
[(2 America), (1 Kenya), (2 Mexico), (4 Saudi arabia), (3 Spain)]
Sort countries by id and name:
[(1 Kenya), (2 America), (2 Mexico), (3 Spain), (4 Saudi arabia)]
Sort with lambda comparator
Coding questions series (YouTube)