我陷入困境,因为我有一个有三个变量的对象书
String title
int Year
String authorName
我必须按升序或降序对一个,两个或所有三个变量进行排序,我实现了标题排序,但当人们选择多个变量进行排序时,我不知道该做什么。
以下是我的一些代码:
书籍课程:
import java.util.ArrayList;
public class Book{
String title;
String authorName;
int editionYear;
public Book(String title, String authorName, int editionYear){
this.title = title;
this.authorName = authorName;
this.editionYear = editionYear;
}
public String getBookInfo(){
ArrayList bookInfo = new ArrayList();
bookInfo.add(this.title);
bookInfo.add(this.authorName);
bookInfo.add(this.editionYear);
return bookInfo.toString();
}
}
BookSorter类:
import java.util.Arrays;
import java.util.Comparator;
public class BookSorter{
private String sortkey;
private String order;
Book[] Books;
public BookSorter(Book Book1, Book Book2, Book Book3, Book Book4){
this.Books = new Book[] {Book1, Book2, Book3, Book4};
}
public Book[] sortByTitle(boolean sortorder){
Comparator<Book> byTitle = new TitleComparator(sortorder);
Arrays.sort(Books, byTitle);
for(int i=0;i<4;i++) System.out.println(Books[i].title);
return Books;
}
}
TitleComparator:
import java.util.Comparator;
class TitleComparator implements Comparator<Book> {
boolean ascending;
public TitleComparator(boolean ascending){
this.ascending = ascending;
}
public int compare(Book Book1, Book Book2){
if(ascending == true){
if(Book1.title.compareToIgnoreCase(Book2.title) > 0) return 1;
else if(Book1.title.compareToIgnoreCase(Book2.title) < 0) return -1;
else return 0;
}else{
if(Book2.title.compareToIgnoreCase(Book1.title) < 0) return -1;
else if(Book2.title.compareToIgnoreCase(Book1.title) > 0) return 1;
else return 0;
}
}
}
我虽然可以在比较器上多做一点但是我真的很困惑如何模拟这样的事情,提前谢谢
答案 0 :(得分:3)
编写3个比较器类,每个比较器类比较一个特定属性,然后编写一个整体比较器类,它采用有序的比较器列表。
或者使用像org.apache.commons.collections.comparators.ComparatorChain这样的库中的一些便利类。
编辑:
OP问道:我怎么能写出整个比较器:
类似的东西:
// private List<Comparator<?>> comparators; // initialized in constructor
// compare method(book1, book2):
// note that while result == 0, books have had equal attributes so far
// once result is != 0, the books are now ordered - no need to compare further
// if we run out of comparators and result still == 0, books are equal.
// initialize iterator to list of comparators
// int result = 0;
// while result == 0 && still more comparators
// get current comparator from iterator
// result = comparator.compare(book1, book2); // compare current attribute
// end-while
// return result
答案 1 :(得分:2)
上升/下降可以更容易实现,因为它简单地“反转”比较结果。您可以“重用”compareToIgnoreCase
方法的结果:
public int compare(Book book1, Book book2) {
int result = book1.title.compareToIgnoreCase(book2.title);
return ascending ? result : result * -1;
}
其他比较器非常相似(将样本限制为比较方法):
public int compare(Book book1, Book book2) {
int result = book1.author.compareToIgnoreCase(book2.author);
return ascending ? result : result * -1;
}
public int compare(Book book1, Book book2) {
Integer year1 = book1.year;
Integer year2 = book2.year;
int result = year1.compareTo(year2);
return ascending ? result : result * -1;
}
答案 2 :(得分:2)
这听起来像是一个家庭作业问题。所以我将为您提供一些提示。
1. First see if Title1==Title2.
1.1 if YES then see if year1==year2
1.1.1 if YES then see if authorName1==authorName2
1.1.1.1 If YES then they are equal (return 0)
1.1.1.2 else if NO compare author1 and author2 (return 1 or -1)
1.2 else if NO then compare year1 and year2 (return 1 or -1)
2. else if NO then compare title1 and title2 (return 1 or -1)
答案 3 :(得分:1)
Group Comparator允许您对多个属性进行排序。您也可以使用Bean Comparator,这样您就不必继续编写自定义比较器。