我有一个类Library,它包含一个Book对象的数组,我需要根据Book的属性(Title或PageNumber)对数组进行排序。问题是我不允许对Book使用Comparable类。你会推荐我如何对Library中的Books数组进行排序?编写自己的排序?或者有更简单的方法吗?如果你需要代码片段,只需问!
c7rzv4ha1#
您可以提供一个Comparator来比较您想要的任何类型,Comparable或其他类型。对于数组和集合,您使用
Comparator
Comparable
Arrays.sort(array, myComparator);Collections.sort(list, myComparator);
Arrays.sort(array, myComparator);
Collections.sort(list, myComparator);
字符串甚至像TreeSet这样的排序集合也可以使用自定义的Comparator例如
Collections.sort(books, new Comparator<Book>() { public int compare(Book b1, Book b2) { return if b1 is greater return +1, if b2 is smaller return -1 otherwise 0 }});
Collections.sort(books, new Comparator<Book>() {
public int compare(Book b1, Book b2) {
return if b1 is greater return +1, if b2 is smaller return -1 otherwise 0
}
});
型
ztigrdn82#
如果你可以使用Comparators,为你需要的每种排序类型写一个,例如,升序为书名,降序为页码。如果第一个参数大于第二个参数,Comparator的compare方法必须返回正数,如果第一个参数小于第二个参数,则返回负数,如果它们相等,则返回零。
Comparators
compare
import java.util.Comparator;import java.util.List;import java.util.Arrays;class Book{ String title; int pageNumber; public Book(String title, int pageNumber){ this.title = title; this.pageNumber = pageNumber; } String getTitle(){ return title; } int getPageNumber(){ return pageNumber; } public String toString(){ return "(" + title + ", " + pageNumber + " pages)"; }}public class Library{ // These variables are static because you don't need multiple copies // for sorting, as they have no intrinsic state. static private Comparator<Book> ascTitle; static private Comparator<Book> descPageNumber; // We initialize static variables inside a static block. static { ascTitle = new Comparator<Book>(){ @Override public int compare(Book b1, Book b2){ return b1.getTitle().compareTo(b2.getTitle()); } }; descPageNumber = new Comparator<Book>(){ @Override public int compare(Book b1, Book b2){ // Java 7 has an Integer#compare function return Integer.compare(b1.getPageNumber(), b2.getPageNumber()); // For Java < 7, use // Integer.valueOf(n1).compareTo(n2); // DO NOT subtract numbers to make a comparison such as n2 - n1. // This can cause a negative overflow if the difference is larger // than Integer.MAX_VALUE (e.g., n1 = 2^31 and n2 = -2^31) } }; } private Book[] books; public Book[] getBooks(){ return books; } public void sortAscTitle(){ Arrays.sort(books, ascTitle); } public void sortDescPageNumber(){ Arrays.sort(books, descPageNumber); } public Library(Book[] books){ this.books = books; } public static void main(String[] args){ Library library = new Library( new Book[]{ new Book("1984", 123), new Book("I, Robot", 152), new Book("Harry Potter and the Philosopher's Stone", 267), new Book("Harry Potter and the Goblet of Fire", 759), new Book("The Bible", 1623) }); library.sortAscTitle(); System.out.println(Arrays.toString(library.getBooks())); library.sortDescPageNumber(); System.out.println(Arrays.toString(library.getBooks())); }}
import java.util.Comparator;
import java.util.List;
import java.util.Arrays;
class Book{
String title;
int pageNumber;
public Book(String title, int pageNumber){
this.title = title;
this.pageNumber = pageNumber;
String getTitle(){ return title; }
int getPageNumber(){ return pageNumber; }
public String toString(){
return "(" + title + ", " + pageNumber + " pages)";
public class Library{
// These variables are static because you don't need multiple copies
// for sorting, as they have no intrinsic state.
static private Comparator<Book> ascTitle;
static private Comparator<Book> descPageNumber;
// We initialize static variables inside a static block.
static {
ascTitle = new Comparator<Book>(){
@Override
public int compare(Book b1, Book b2){
return b1.getTitle().compareTo(b2.getTitle());
};
descPageNumber = new Comparator<Book>(){
// Java 7 has an Integer#compare function
return Integer.compare(b1.getPageNumber(), b2.getPageNumber());
// For Java < 7, use
// Integer.valueOf(n1).compareTo(n2);
// DO NOT subtract numbers to make a comparison such as n2 - n1.
// This can cause a negative overflow if the difference is larger
// than Integer.MAX_VALUE (e.g., n1 = 2^31 and n2 = -2^31)
private Book[] books;
public Book[] getBooks(){ return books; }
public void sortAscTitle(){
Arrays.sort(books, ascTitle);
public void sortDescPageNumber(){
Arrays.sort(books, descPageNumber);
public Library(Book[] books){
this.books = books;
public static void main(String[] args){
Library library = new Library( new Book[]{
new Book("1984", 123),
new Book("I, Robot", 152),
new Book("Harry Potter and the Philosopher's Stone", 267),
new Book("Harry Potter and the Goblet of Fire", 759),
new Book("The Bible", 1623)
library.sortAscTitle();
System.out.println(Arrays.toString(library.getBooks()));
library.sortDescPageNumber();
字符串
1yjd4xko3#
把这个放在你的图书馆里:
java.util.Collections.sort(bookList, bookComparator);
6ovsh4lw4#
扩展@PeterLawrey对Java 8的回答,现在可以使用Lambda Expression而不是Comparable<T>委托:
Comparable<T>
Collections.sort(books, (firstBook, secondBook -> b1 is greater return +1, if b2 is smaller return -1 otherwise 0));
Collections.sort(books, (firstBook, secondBook -> b1 is greater return +1,
if b2 is smaller return -1 otherwise 0));
4条答案
按热度按时间c7rzv4ha1#
您可以提供一个
Comparator
来比较您想要的任何类型,Comparable
或其他类型。对于数组和集合,您使用
字符串
甚至像TreeSet这样的排序集合也可以使用自定义的Comparator
例如
型
ztigrdn82#
如果你可以使用
Comparators
,为你需要的每种排序类型写一个,例如,升序为书名,降序为页码。如果第一个参数大于第二个参数,Comparator
的compare
方法必须返回正数,如果第一个参数小于第二个参数,则返回负数,如果它们相等,则返回零。字符串
1yjd4xko3#
把这个放在你的图书馆里:
字符串
6ovsh4lw4#
扩展@PeterLawrey对Java 8的回答,现在可以使用Lambda Expression而不是
Comparable<T>
委托:字符串