Java Sorting:按属性对对象数组进行排序,对象不允许使用Comparable

zlwx9yxi  于 2024-01-05  发布在  Java
关注(0)|答案(4)|浏览(197)

我有一个类Library,它包含一个Book对象的数组,我需要根据Book的属性(Title或PageNumber)对数组进行排序。问题是我不允许对Book使用Comparable类。你会推荐我如何对Library中的Books数组进行排序?编写自己的排序?或者有更简单的方法吗?如果你需要代码片段,只需问!

c7rzv4ha

c7rzv4ha1#

您可以提供一个Comparator来比较您想要的任何类型,Comparable或其他类型。
对于数组和集合,您使用

  1. Arrays.sort(array, myComparator);
  2. Collections.sort(list, myComparator);

字符串
甚至像TreeSet这样的排序集合也可以使用自定义的Comparator
例如

  1. Collections.sort(books, new Comparator<Book>() {
  2. public int compare(Book b1, Book b2) {
  3. return if b1 is greater return +1, if b2 is smaller return -1 otherwise 0
  4. }
  5. });

展开查看全部
ztigrdn8

ztigrdn82#

如果你可以使用Comparators,为你需要的每种排序类型写一个,例如,升序为书名,降序为页码。如果第一个参数大于第二个参数,Comparatorcompare方法必须返回正数,如果第一个参数小于第二个参数,则返回负数,如果它们相等,则返回零。

  1. import java.util.Comparator;
  2. import java.util.List;
  3. import java.util.Arrays;
  4. class Book{
  5. String title;
  6. int pageNumber;
  7. public Book(String title, int pageNumber){
  8. this.title = title;
  9. this.pageNumber = pageNumber;
  10. }
  11. String getTitle(){ return title; }
  12. int getPageNumber(){ return pageNumber; }
  13. public String toString(){
  14. return "(" + title + ", " + pageNumber + " pages)";
  15. }
  16. }
  17. public class Library{
  18. // These variables are static because you don't need multiple copies
  19. // for sorting, as they have no intrinsic state.
  20. static private Comparator<Book> ascTitle;
  21. static private Comparator<Book> descPageNumber;
  22. // We initialize static variables inside a static block.
  23. static {
  24. ascTitle = new Comparator<Book>(){
  25. @Override
  26. public int compare(Book b1, Book b2){
  27. return b1.getTitle().compareTo(b2.getTitle());
  28. }
  29. };
  30. descPageNumber = new Comparator<Book>(){
  31. @Override
  32. public int compare(Book b1, Book b2){
  33. // Java 7 has an Integer#compare function
  34. return Integer.compare(b1.getPageNumber(), b2.getPageNumber());
  35. // For Java < 7, use
  36. // Integer.valueOf(n1).compareTo(n2);
  37. // DO NOT subtract numbers to make a comparison such as n2 - n1.
  38. // This can cause a negative overflow if the difference is larger
  39. // than Integer.MAX_VALUE (e.g., n1 = 2^31 and n2 = -2^31)
  40. }
  41. };
  42. }
  43. private Book[] books;
  44. public Book[] getBooks(){ return books; }
  45. public void sortAscTitle(){
  46. Arrays.sort(books, ascTitle);
  47. }
  48. public void sortDescPageNumber(){
  49. Arrays.sort(books, descPageNumber);
  50. }
  51. public Library(Book[] books){
  52. this.books = books;
  53. }
  54. public static void main(String[] args){
  55. Library library = new Library( new Book[]{
  56. new Book("1984", 123),
  57. new Book("I, Robot", 152),
  58. new Book("Harry Potter and the Philosopher's Stone", 267),
  59. new Book("Harry Potter and the Goblet of Fire", 759),
  60. new Book("The Bible", 1623)
  61. });
  62. library.sortAscTitle();
  63. System.out.println(Arrays.toString(library.getBooks()));
  64. library.sortDescPageNumber();
  65. System.out.println(Arrays.toString(library.getBooks()));
  66. }
  67. }

字符串

展开查看全部
1yjd4xko

1yjd4xko3#

把这个放在你的图书馆里:

  1. java.util.Collections.sort(bookList, bookComparator);

字符串

6ovsh4lw

6ovsh4lw4#

扩展@PeterLawrey对Java 8的回答,现在可以使用Lambda Expression而不是Comparable<T>委托:

  1. Collections.sort(books, (firstBook, secondBook -> b1 is greater return +1,
  2. if b2 is smaller return -1 otherwise 0));

字符串

相关问题