Java 使用Arrays.sort()方法对对象类型的数组进行排序

Java 使用Arrays.sort()方法对对象类型的数组进行排序,java,comparison,sorting,Java,Comparison,Sorting,我知道如何使用Arrays.sort()方法按以下方式对对象数组进行排序 Arrays.sort(array of primitive type); Arrays.sort(array of primitive type, from, to); Arrays.sort(array of an object type); Arrays.sort(array of an object type , from, to); 但我不知道以下两种方法 Arrays.sort(arra

我知道如何使用Arrays.sort()方法按以下方式对对象数组进行排序

Arrays.sort(array of primitive type);   
Arrays.sort(array of primitive type, from, to); 
Arrays.sort(array of an object type);   
Arrays.sort(array of an object type , from, to);    
但我不知道以下两种方法

Arrays.sort(array of an object type , comparator);  
Arrays.sort(array of an object type , from, to, comparator);    
有人能告诉我如何使用这些方法对类型对象数组进行排序吗?我请求您添加代码或指向.java类的任何链接。我试图搜索它,但找不到它

谢谢。

示例:

class Person{  
   int id;  
   public getId(){return this.id;}  
//Other  stuff in your custom class
}  

Person[] persons = ...;//An array of person you get from somewhere
Arrays.sort(persons,new Comparator<Person>(){  
    @Override  
    public int compare(Person p1, Person p2){  
         return p1.getId() - p2.getId();  
   }  
} ); 
class-Person{
int-id;
public getId(){返回this.id;}
//自定义类中的其他内容
}  
人[]人=//你从某处得到的一系列人
Arrays.sort(persons,new Comparator(){
@凌驾
公共整数比较(人p1,人p2){
返回p1.getId()-p2.getId();
}  
} ); 

下面是一个比较器未内联定义的示例

任何一种方式都可以接受,但我认为这种方式更容易理解

class Person {
   int id;  
   public getId(){
       return this.id;
   }  
}

class PersonComparator implements Comparator<Person> {
    @Override
    public int compareTo(Person personOne, Person personTwo) {
        reuturn personOne.getId() - personTwo.getId();
    }
}
Comparator是一个只有一种方法的接口:compareTo

创建比较器时,这是您需要实现的唯一方法

请注意,PersonComparator.compareTo()只返回两个Person对象的ID之差

这是因为compareTo()方法的工作原理:

  • 如果第一项“在”第二项之前,则应返回负数
  • 如果第一项“在”第二项之后,则应返回正数
  • 如果两个项目相等(在订购方面),则应返回零

查看文档了解更多信息…

对于复杂对象,Java不知道如何比较它们。因此,您需要编写一个比较器。通常,您选择要比较的类的一个成员

public class Comp implements Comparator<Test> {

    @Override
    public int compare(Test t, Test t1) {
       return what_you_want_to_compare;
    }    
}
公共类Comp实现比较器{
@凌驾
公共整数比较(测试t、测试t1){
返回要比较的内容;
}    
}
这很简单:

比较器接口使您可以控制对对象进行排序的方式

一个对象可以基于一个关键是你的智慧

例如,应根据AccountNumber对Account对象进行排序

class Account {
    String AccountNumber; //Key 1 
    String AccountName;   //Key 2
    String GovtID;        //Key 3 
}
您可以按三个键中的任意一个进行排序

为了控制排序,您必须定义一个实现Comparator接口的类,该接口将定义用于排序的逻辑

class SortAccountByNumber implements Comparator<Account> {
    //Implement Unimplemented method 
    @Override
    public int compare(Account a1, Account a2) {
        //Read the specification for this method here in the Java Doc.
        return 0;
    }

}

请查看有关的文档。您需要实现一个比较器来使用您所询问的方法。找到给定java类的文档应该非常容易。你所需要做的就是谷歌
Java7类名
。在本例中,google:
java7comparator
。如果您正在查看
数组
类的,则有多个指向
比较器
文档的链接。
class SortAccountByNumber implements Comparator<Account> {
    //Implement Unimplemented method 
    @Override
    public int compare(Account a1, Account a2) {
        //Read the specification for this method here in the Java Doc.
        return 0;
    }

}
  SortAccountByNumber varSortAccountByNumber = new SortAccountByNumber();
  Arrays.sort(arrayOfAccounts,varSortAccountByNumber);