使用Arrays.sort()方法排序对象类型的数组 [英] sorting array of type object using Arrays.sort() method

查看:873
本文介绍了使用Arrays.sort()方法排序对象类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to sort an array of objects using Arrays.sort() method in following way.

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);    

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

but i have no idea about following two methods.

Arrays.sort(array of an object type , comparator);  
Arrays.sort(array of an object type , from, to, comparator);    

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

Can someone please let me know how to sort array of type object using these methods.I request you to add code or any link that directs to the .java class.I tried to search it but could not find it.

谢谢。

推荐答案

示例:

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();  
   }  
} ); 

这篇关于使用Arrays.sort()方法排序对象类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆