-3

我有一个数组author-->bookName,我想用 bookName(s) 对其进行排序。我需要在 Java 中排序bookName-->author(通讯作者姓名)的答案

编辑:评论中的代码:

Arrays.sort(book, new Comparator<LibraryBook>() { 
    public int compare(LibraryBook arg0, LibraryBook arg1) { 
        return arg0.getBookTitle().compareTo(arg1.getBookTitle()); 
    }
}); 
printBooks(book); 

private static void printBooks(LibraryBook[] book){ 
    for (int x = 0; x < book.length; ++x) { 
        String title = book[x].getBookTitle(); 
        String author = book[x].getBookAuthor(); 
        System.out.println(String.format("%30s%30s", title, author)); 
    }
}
4

1 回答 1

0

你需要的是Arrays.sort()两个比较器。

Arrays.sort(yourArray, new Comparator<YourObject>() {
    public int compare(YourObject o1, YourObject o2) {
        // here use author or bookname
    }
});
于 2012-12-09T08:05:15.183 回答