集册 Java实例教程 使用比较器对象对对象进行排序

使用比较器对象对对象进行排序

欢马劈雪     最近更新时间:2020-01-02 10:19:05

573
使用比较器对象对对象排序

import java.util.Arrays;

import java.util.Comparator;

/* 
 来自 
*时 代      J a v a   公   众 号 - nowjava.com*/

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;

  }

}


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 {/*来自 N o w J a v a . c o m - 时  代  Java*/

  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() {

    
展开阅读全文