原文: https://beginnersbook.com/2017/08/comparator-interface-in-java/

最后一个教程中,我们已经了解了如何使用Comparable接口对自定义类的对象进行排序。通过使用Comparable,我们可以根据任何数据成员对对象进行排序。例如,假设我们有一个Author类有数据成员:作者姓名,书名和作者年龄,现在如果我们想根据任何数据成员对对象进行排序那么我们可以使用Comparable但是如果我们想要有多个排序选项,并且我们可以根据任何选择对对象进行排序,这可以使用Comparator接口完成,我们可以创建尽可能多的Comparator然后我们可以在一个或多个上调用Collections.sort这样的比较器:

  1. //Sorting arraylist al by Author Age
  2. Collections.sort(al, new AuthorAgeComparator());
  3. //Sorting arraylist al by Book Name
  4. Collections.sort(al, new BookNameComparator());

那么它是怎样工作的?要像这样调用Collections.sort方法,我们必须首先编写这些ComparatorAuthorAgeComparatorBookNameComparator,以及Author类和Main类。

完整的比较示例

Author.java

  1. public class Author implements Comparable<Author> {
  2. String firstName;
  3. String bookName;
  4. int auAge;
  5. Author(String first, String book, int age){
  6. this.firstName = first;
  7. this.bookName = book;
  8. this.auAge = age;
  9. }
  10. public String getFirstName() {
  11. return firstName;
  12. }
  13. public void setFirstName(String firstName) {
  14. this.firstName = firstName;
  15. }
  16. public String getBookName() {
  17. return bookName;
  18. }
  19. public void setBookName(String bookName) {
  20. this.bookName = bookName;
  21. }
  22. public int getAuAge() {
  23. return auAge;
  24. }
  25. public void setAuAge(int auAge) {
  26. this.auAge = auAge;
  27. }
  28. @Override
  29. /*
  30. * When we only use Comparable, this is where we write sorting
  31. * logic. This method is called when we implement the Comparable
  32. * interface in our class and call Collections.sort()
  33. */
  34. public int compareTo(Author au){
  35. return this.firstName.compareTo(au.firstName);
  36. }
  37. }

AuthorAgeComparator.java

  1. import java.util.*;
  2. class AuthorAgeComparator implements Comparator<Author>{
  3. public int compare(Author a1,Author a2){
  4. if(a1.auAge==a2.auAge)
  5. return 0;
  6. else if(a1.auAge>a2.auAge)
  7. return 1;
  8. else
  9. return -1;
  10. }
  11. }

BookNameComparator.java

  1. import java.util.*;
  2. public class BookNameComparator implements Comparator<Author>{
  3. public int compare(Author a1,Author a2){
  4. return a1.bookName.compareTo(a2.bookName);
  5. }
  6. }

SortingPgm.java

  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. public class SortingPgm{
  4. public static void main(String args[]){
  5. // List of objects of Author class
  6. ArrayList<Author> al=new ArrayList<Author>();
  7. al.add(new Author("Henry", "Tropic of Cancer", 45));
  8. al.add(new Author("Nalo", "Brown Girl in the Ring", 56));
  9. al.add(new Author("Frank", "300", 65));
  10. al.add(new Author("Deborah", "Sky Boys", 51));
  11. al.add(new Author("George R. R.", "A Song of Ice and Fire", 62));
  12. /*
  13. * Sorting the list using Collections.sort() method, we
  14. * can use this method because we have implemented the
  15. * Comparable interface in our user defined class Author
  16. */
  17. System.out.println("Sorting by Author First Name:");
  18. Collections.sort(al);
  19. for(Author au: al){
  20. System.out.println(au.getFirstName()+", "+au.getBookName()+", "+
  21. au.getAuAge());
  22. }
  23. /*Sorting using AuthorAgeComparator*/
  24. System.out.println("Sorting by Author Age:");
  25. Collections.sort(al, new AuthorAgeComparator());
  26. for(Author au: al){
  27. System.out.println(au.getFirstName()+", "+au.getBookName()+", "+
  28. au.getAuAge());
  29. }
  30. /*Sorting using BookNameComparator*/
  31. System.out.println("Sorting by Book Name:");
  32. Collections.sort(al, new BookNameComparator());
  33. for(Author au: al){
  34. System.out.println(au.getFirstName()+", "+au.getBookName()+", "+
  35. au.getAuAge());
  36. }
  37. }
  38. }

输出:

  1. Sorting by Author First Name:
  2. Deborah, Sky Boys, 51
  3. Frank, 300, 65
  4. George R. R., A Song of Ice and Fire, 62
  5. Henry, Tropic of Cancer, 45
  6. Nalo, Brown Girl in the Ring, 56
  7. Sorting by Author Age:
  8. Henry, Tropic of Cancer, 45
  9. Deborah, Sky Boys, 51
  10. Nalo, Brown Girl in the Ring, 56
  11. George R. R., A Song of Ice and Fire, 62
  12. Frank, 300, 65
  13. Sorting by Book Name:
  14. Frank, 300, 65
  15. George R. R., A Song of Ice and Fire, 62
  16. Nalo, Brown Girl in the Ring, 56
  17. Deborah, Sky Boys, 51
  18. Henry, Tropic of Cancer, 45