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

nak*_*kul 3 java sorting comparison

我知道如何使用 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);    
Run Code Online (Sandbox Code Playgroud)

但我不知道以下两种方法。

Arrays.sort(array of an object type , comparator);  
Arrays.sort(array of an object type , from, to, comparator);    
Run Code Online (Sandbox Code Playgroud)

有人可以让我知道如何使用这些方法对类型对象的数组进行排序吗?我请求您添加代码或任何指向 .java 类的链接。我尝试搜索它但找不到它。

谢谢。

Cra*_*lus 5

例子:

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();  
   }  
} ); 
Run Code Online (Sandbox Code Playgroud)