如何sortingJava中的对象数组?

我的数组不包含任何string。 但是它包含对象引用。 每个对象引用通过toString方法返回name,id,author和publisher。

public String toString() { return (name + "\n" + id + "\n" + author + "\n" + publisher + "\n"); } 

现在我需要按名称sorting该对象的数组。 我知道如何sorting,但我不知道如何从对象中提取名称并对其进行sorting。

你有两种方法来做到这一点,都使用数组工具类

  1. 实现一个比较器 ,并将您的数组和比较器一起传递给sorting方法 ,将其作为第二个参数。
  2. 在对象所属的类中实现Comparable接口,并将您的数组传递给仅使用一个参数的sorting方法 。

 class Book implements Comparable<Book> { public String name, id, author, publisher; public Book(String name, String id, String author, String publisher) { this.name = name; this.id = id; this.author = author; this.publisher = publisher; } public String toString() { return ("(" + name + ", " + id + ", " + author + ", " + publisher + ")"); } @Override public int compareTo(Book o) { // usually toString should not be used, // instead one of the attributes or more in a comparator chain return toString().compareTo(o.toString()); } } @Test public void sortBooks() { Book[] books = { new Book("foo", "1", "author1", "pub1"), new Book("bar", "2", "author2", "pub2") }; // 1. sort using Comparable Arrays.sort(books); System.out.println(Arrays.asList(books)); // 2. sort using comparator: sort by id Arrays.sort(books, new Comparator<Book>() { @Override public int compare(Book o1, Book o2) { return o1.id.compareTo(o2.id); } }); System.out.println(Arrays.asList(books)); } 

产量

 [(bar, 2, author2, pub2), (foo, 1, author1, pub1)] [(foo, 1, author1, pub1), (bar, 2, author2, pub2)] 

你可以尝试这样的事情:

 List<Book> books = new ArrayList<Book>(); Collections.sort(books, new Comparator<Book>(){ public int compare(Book o1, Book o2) { return o1.name.compareTo(o2.name); } }); 

Java 8


使用lambdaexpression式

 Arrays.sort(myTypes, (a,b) -> a.name.compareTo(b.name)); 

Test.java

 public class Test { public static void main(String[] args) { MyType[] myTypes = { new MyType("John", 2, "author1", "publisher1"), new MyType("Marry", 298, "author2", "publisher2"), new MyType("David", 3, "author3", "publisher3"), }; System.out.println("--- before"); System.out.println(Arrays.asList(myTypes)); Arrays.sort(myTypes, (a, b) -> a.name.compareTo(b.name)); System.out.println("--- after"); System.out.println(Arrays.asList(myTypes)); } } 

MyType.java

 public class MyType { public String name; public int id; public String author; public String publisher; public MyType(String name, int id, String author, String publisher) { this.name = name; this.id = id; this.author = author; this.publisher = publisher; } @Override public String toString() { return "MyType{" + "name=" + name + '\'' + ", id=" + id + ", author='" + author + '\'' + ", publisher='" + publisher + '\'' + '}' + System.getProperty("line.separator"); } } 

输出:

 --- before [MyType{name=John', id=2, author='author1', publisher='publisher1'} , MyType{name=Marry', id=298, author='author2', publisher='publisher2'} , MyType{name=David', id=3, author='author3', publisher='publisher3'} ] --- after [MyType{name=David', id=3, author='author3', publisher='publisher3'} , MyType{name=John', id=2, author='author1', publisher='publisher1'} , MyType{name=Marry', id=298, author='author2', publisher='publisher2'} ] 

使用方法引用

 Arrays.sort(myTypes, MyType::compareThem); 

compareThem必须在MyType.java中添加:

 public static int compareThem(MyType a, MyType b) { return a.name.compareTo(b.name); }