Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)

控制台程序。

Arrays类中的sort()静态方法把传送为参数的数组元素按升序方式排序。

对于第一个参数类型是Object[]的sort()方法来说,可以传送任意类型的数组。如果使用sort()方法的任何一个版本对对象数组排序,对象就必须支持Comparable<>接口,因为sort()方法使用了compareTo()方法。

sort()方法的另外两个用来对对象数组排序的版本是参数化方法。它们在对数组排序是,使用外部的比较器对象来确定对象的顺序。比较器对象的类必须实现java.util.Comparator<>接口。使用外部比较器的优点是:可以根据不同的情况使用几个比较器得到不同的顺序。例如,在一些情况下,要按照名字对姓名文件排序;而在另外一些情况下,则要按照姓氏排序。此时不能使用类已经实现的Comparable<>接口。使用比较器的sort()方法的第一个版本是:

<T>void sort(T[] array,Comparator<? super T> comparator)

这个方法会使用传送为第二个参数的比较器对array的所有元素排序。

Comparator<T>接口声明了两个方法。第一个方法是compare(),sort()方法使用它来比较T[]类型的数组元素。第二个方法是equals(),用于比较Comparator<>对象是否相等。

 1 public class Person implements Comparable<Person> {
 2   // Constructor
 3   public Person(String firstName, String surname) {
 4     this.firstName = firstName;
 5     this.surname = surname;
 6   }
 7 
 8   @Override
 9   public String toString() {
10     return firstName + " " + surname;
11   }
12 
13   // Compare Person objects
14   public int compareTo(Person person) {
15     int result = surname.compareTo(person.surname);
16     return result == 0 ? firstName.compareTo(person.firstName):result;
17   }
18 
19   public String getFirstName() {
20     return firstName;
21   }
22 
23   public String getSurname() {
24     return surname;
25   }
26   private String firstName;                                            // First name of person
27   private String surname;                                              // Second name of person
28 }
 1 import java.util.Comparator;
 2 
 3 public class ComparePersons implements Comparator<Person> {
 4   // Method to compare Person objects - order is descending
 5   public int compare(Person person1, Person person2) {
 6     int result = -person1.getSurname().compareTo(person2.getSurname());
 7     return result == 0 ? -person1.getFirstName().compareTo(person2.getFirstName()) : result;
 8   }
 9 
10   // Method to compare with another comparator
11   public boolean equals(Object comparator) {
12     if(this == comparator) {                                           // If argument is the same object
13       return true;                                                     // then it must be equal
14     }
15     if(comparator == null) {                                           // If argument is null
16       return false;                                                    // then it can抰 be equal
17     }
18     return getClass() == comparator.getClass();                        // Class must be the same for equal
19   }
20 }
 1 import java.util.Arrays;
 2 
 3 public class TrySortingWithComparator {
 4   public static void main(String[] args) {
 5     Person[] authors = {
 6             new Person("Danielle", "Steel"), new Person("John", "Grisham"),
 7             new Person("Tom", "Clancy"),     new Person("Christina", "Schwartz"),
 8             new Person("Patricia", "Cornwell"), new Person("Bill", "Bryson")
 9                        };
10 
11     System.out.println("Original order:");
12     for(Person author : authors) {
13       System.out.println(author);
14     }
15 
16     Arrays.sort(authors, new ComparePersons());                        // Sort using comparator
17 
18     System.out.println("
Order after sorting using comparator:");
19     for(Person author : authors) {
20       System.out.println(author);
21     }
22 
23     Arrays.sort(authors);                                              // Sort using compareTo() method
24 
25     System.out.println("
Order after sorting using compareTo() method:");
26     for(Person author : authors) {
27       System.out.println(author);
28     }
29   }
30 }
原文地址:https://www.cnblogs.com/mannixiang/p/3432473.html