您的位置:首页 > 编程语言 > Java开发

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

2013-11-19 23:09 615 查看
控制台程序。

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<>对象是否相等。

public class Person implements Comparable<Person> {
// Constructor
public Person(String firstName, String surname) {
this.firstName = firstName;
this.surname = surname;
}

@Override
public String toString() {
return firstName + " " + surname;
}

// Compare Person objects
public int compareTo(Person person) {
int result = surname.compareTo(person.surname);
return result == 0 ? firstName.compareTo(person.firstName):result;
}

public String getFirstName() {
return firstName;
}

public String getSurname() {
return surname;
}
private String firstName;                                            // First name of person
private String surname;                                              // Second name of person
}


import java.util.Comparator;

public class ComparePersons implements Comparator<Person> {
// Method to compare Person objects - order is descending
public int compare(Person person1, Person person2) {
int result = -person1.getSurname().compareTo(person2.getSurname());
return result == 0 ? -person1.getFirstName().compareTo(person2.getFirstName()) : result;
}

// Method to compare with another comparator
public boolean equals(Object comparator) {
if(this == comparator) {                                           // If argument is the same object
return true;                                                     // then it must be equal
}
if(comparator == null) {                                           // If argument is null
return false;                                                    // then it can抰 be equal
}
return getClass() == comparator.getClass();                        // Class must be the same for equal
}
}


import java.util.Arrays;

public class TrySortingWithComparator {
public static void main(String[] args) {
Person[] authors = {
new Person("Danielle", "Steel"), new Person("John", "Grisham"),
new Person("Tom", "Clancy"),     new Person("Christina", "Schwartz"),
new Person("Patricia", "Cornwell"), new Person("Bill", "Bryson")
};

System.out.println("Original order:");
for(Person author : authors) {
System.out.println(author);
}

Arrays.sort(authors, new ComparePersons());                        // Sort using comparator

System.out.println("\nOrder after sorting using comparator:");
for(Person author : authors) {
System.out.println(author);
}

Arrays.sort(authors);                                              // Sort using compareTo() method

System.out.println("\nOrder after sorting using compareTo() method:");
for(Person author : authors) {
System.out.println(author);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐