定制对ArrayList的sort方法的自定义排序
java中的ArrayList需要通过collections类的sort方法来进行排序
如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法
调用sort方法时将ArrayList对象与实现Commparator接口的类的对象作为参数
示例:
// 外部类的方式
import java.util.ArrayList; import java.util.Collections;
import java.util.Comparator; import java.util.List;
public class Test { public static void main(String[] args) {
Student zlj = new Student("丁晓宇", 21);
Student dxy = new Student("赵四", 22);
Student cjc = new Student("张三", 11);
Student lgc = new Student("刘武", 19);
List<Student> studentList = new ArrayList<Student>();
studentList.add(zlj);
studentList.add(dxy);
studentList.add(cjc);
studentList.add(lgc);
Collections.sort(studentList, new SortByAge());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
}
System.out.println(" = ");
Collections.sort(studentList, new SortByName());
for (Student student : studentList) {
System.out.println(student.getName() + " / " + student.getAge());
} } }
class SortByAge implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1; Student s2 = (Student) o2;
if (s1.getAge() > s2.getAge()) return 1; return 0; } }
class SortByName implements Comparator {
public int compare(Object o1, Object o2) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getName().compareTo(s2.getName()); } }
//匿名内部类的方式
//按照受益人级别进行排序
Collections.sort(beneficiarys,new Comparator(){
@Override
public int compare(Object o1, Object o2) {
Beneficiary s1 = (Beneficiary) o1;
Beneficiary s2 = (Beneficiary) o2;
if (Integer.parseInt(s1.getBeneficiaryOrder()) > Integer.parseInt(s2.getBeneficiaryOrder()))
return 1;
return 0;
} });
定制对ArrayList的sort方法的自定义排序的更多相关文章
- java中sort方法的自定义比较器写法(转载)
java中sort方法的自定义比较器写法 摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collecti ...
- 用Java集合中的Collections.sort方法对list排序的两种方法
用Collections.sort方法对list排序有两种方法第一种是list中的对象实现Comparable接口,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- java中Collections.sort()方法实现集合排序
1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>(); //定 ...
- 【C++】标准库sort函数的自定义排序
自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...
- (JavaScript基础向)sort()方法里的排序函数的理解
比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...
- Collections.sort方法对list排序的两种方式
Collections.sort( )分为两部分,一部分为排序规则,一部分为排序算法 . 规则用来判断对象,算法则考虑如何进行排序 对于自定义对象,sort()不知道规则,所以无法比较,这种情况下一定 ...
- Java基础集锦——利用Collections.sort方法对list排序
要想对List进行排序,可以让实体对象实现Comparable接口,重写compareTo方法即可实现按某一属性排序,但是这种写法很单一,只能按照固定的一个属性排序,没变法变化.通过下面这种方法,可以 ...
- ArrayList 排序Sort()方法扩展
1.sort() sort可以直接对默认继承 IComparable接口的类进行排序,如:int.string.... ArrayList arrayList = new ArrayList(); , ...
随机推荐
- python 动态加载module、class、function
python作为一种动态解释型语言,在实现各种框架方面具有很大的灵活性. 最近在研究python web框架,发现各种框架中需要显示的定义各种路由和Handler的映射,如果想要实现并维护复杂的web ...
- razor 添加html5属性
在 HTML5 中, 可以使用 data- 属性来表示用户数据,这些数据甚至可以是 JSON 格式的数据,对 Web 前端开发带来很大的方便. 在 MVC 的 Razor 中,可以使用匿名对象来生成定 ...
- Java Map遍历方式的选择
[原文] 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keyS ...
- 【转】 使用Redis的Pub/Sub来实现类似于JMS的消息持久化
http://blog.csdn.net/canot/article/details/52040415 关于个人对Redis提供的Pub/Sub机制的认识在上一篇博客中涉及到了,也提到了关于如何避免R ...
- 转:VS2010调试NUnit测试项目 (Running or debugging NUnit tests from Visual Studio without any extensions)
If you write unit tests and use NUnit test framework this may be helpful. I decided to write this si ...
- 让android webView使用系统默认浏览器内核直接解析,不弹出选择浏览器选项
遇到一个需求,要求浏览网页的页面不去启动其他的浏览器,全部在自身的应用中. 解决方法 webview.setWebViewClient(new WebViewClient() { @Override ...
- SQL重要命令
SQL对大小写不敏感:SELECT与select是相同的. SQL语句的分号:分号是在数据库系统中分隔每条 SQL 语句的标准方法,这样就可以在对服务器的相同请求中执行一条以上的 SQL 语句. SE ...
- iOS Plist文件,增删改查
今天早上,9点开始弄Plist,然后一直写,一直写(中午取出40分钟吃饭时间),写到1点,写完了,交给头,头说,不是这个意思.我是每个用户创建了一个文件夹,在这个用户的文件夹里面,分别根据应用创建了文 ...
- HTML5列表、块和布局
一.列表 <ul> <ol> <li> unorder order list ol: type="1 A a I i" start=" ...
- js的语句
1.语句类型总结 var box = 100;//单行语句 { //用花括号包含的语句集合,叫做复合语句,单位一个 //一对花括号,表示一个复合语句,处理时,可以单做一条单行语句, //复合语句,我们 ...