Java - Interface Comparator Interface

What is Comparator Interface?

Comparable interface imposes a specified ordering on objects of a class.

Syntax

The Comparator interface is a generic interface.

public interface Comparator<T> {
        int compare(T o1, T o2);
        boolean equals(Object obj);

        ...
}

Demo

import java.util.Arrays;
import java.util.Comparator;

class FirstNameComparator implements Comparator<ComparablePerson> {
  public int compare(ComparablePerson p1, ComparablePerson p2) {
    String firstName1 = p1.getFirstName();
    String firstName2 = p2.getFirstName();
    int diff = firstName1.compareTo(firstName2);
    return diff;//from   w  w  w .  ja  va  2  s.  c o  m
  }
}

class LastNameComparator implements Comparator<ComparablePerson> {
  public int compare(ComparablePerson p1, ComparablePerson p2) {
    String lastName1 = p1.getLastName();
    String lastName2 = p2.getFirstName();
    int diff = lastName1.compareTo(lastName2);
    return diff;
  }
}

class ComparablePerson implements Comparable<ComparablePerson> {
  private String firstName;
  private String lastName;

  public ComparablePerson(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  // Compares two persons based on their last names. If last names are
  // the same, use first names
  public int compareTo(ComparablePerson anotherPerson) {
    int diff = getLastName().compareTo(anotherPerson.getLastName());
    if (diff == 0) {
      diff = getFirstName().compareTo(anotherPerson.getFirstName());
    }
    return diff;
  }

  public String toString() {
    return getLastName() + ", " + getFirstName();
  }
}

public class Main {
  public static void main(String[] args) {
    ComparablePerson[] persons = new ComparablePerson[] {
        new ComparablePerson("A", "X"),
        new ComparablePerson("B", "Y"),
        new ComparablePerson("C", "Z") };

    System.out.println("Original array...");
    print(persons);

    // Sort using first name
    Comparator<ComparablePerson> fnComparator = new FirstNameComparator();
    Arrays.sort(persons, fnComparator);
    System.out.println("\nAfter sorting on first name...");
    print(persons);

    // Sort using last name
    Comparator<ComparablePerson> lnComparator = new LastNameComparator();
    Arrays.sort(persons, lnComparator);
    System.out.println("\nAfter sorting on last name...");
    print(persons);

    // Sort using first name, then last name
    Comparator<ComparablePerson> firstLastComparator = fnComparator
        .thenComparing(lnComparator);
    Arrays.sort(persons, firstLastComparator);
    System.out.println("\nAfter sorting on first, then last name...");
    print(persons);

    // Sort using first name, then last name in reversed order
    Comparator<ComparablePerson> firstLastReverseComparator = firstLastComparator
        .reversed();

    Arrays.sort(persons, firstLastReverseComparator);
    System.out
        .println("\nAfter sorting on first, then last name in reversed...");
    print(persons);

    // Sort using first name, then last name using null first
    Comparator<ComparablePerson> nullFirstComparator = Comparator
        .nullsFirst(firstLastComparator);
    ComparablePerson[] personsWithNulls = new ComparablePerson[] {
        new ComparablePerson("A", "X"), null,
        new ComparablePerson("B", "Y"),
        new ComparablePerson("C", "Z"), null };

    Arrays.sort(personsWithNulls, nullFirstComparator);
    System.out.println("\nAfter sorting on first, then last name "
        + "using null first...");
    print(personsWithNulls);
  }

  public static void print(ComparablePerson[] persons) {
    for (ComparablePerson person : persons) {
      System.out.println(person);
    }
  }
}

Result