Uses a Comparator Object to Sort Objects - Java Collection Framework

Java examples for Collection Framework:Comparator

Description

Uses a Comparator Object to Sort Objects

Demo Code

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

class FirstNameComparator implements Comparator<Person> {
  public int compare(Person p1, Person p2) {
    String firstName1 = p1.getFirstName();
    String firstName2 = p2.getFirstName();
    int diff = firstName1.compareTo(firstName2);
    return diff;//w  w w  .j  a  v a 2  s.c  om
  }
}

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

public class Main {
  public static void main(String[] args) {
    Person[] persons = new Person[] { new Person("A", "C"),
        new Person("Jeff", "Jacobs"), new Person("B", "D") };

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

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

    // Sort using last name
    Comparator<Person> 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<Person> 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<Person> 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<Person> nullFirstComparator = Comparator
        .nullsFirst(firstLastComparator);
    Person[] personsWithNulls = new Person[] { new Person("A", "A"),
        null, new Person("C", "C"), new Person("B", "B"), null };

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

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

class Person implements Comparable<Person> {

  private String firstName;
  private String lastName;

  public Person(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(Person anotherPerson) {
    int diff = getLastName().compareTo(anotherPerson.getLastName());
    if (diff == 0) {
      diff = getFirstName().compareTo(anotherPerson.getFirstName());
    }
    return diff;
  }

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

Result


Related Tutorials