对任意类型集合对象进行整体排序,排序时将此接口的实现传递给Collections.sort方法或者Arrays.sort方法排序.
实现int compare(T o1, T o2);方法,返回正数,零,负数各代表大于,等于,小于。

单一条件排序:

		List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30));
add(new Student("李四", 20));
add(new Student("王五", 60));
}
};
//对users按年龄进行排序
Collections.sort(stus, new Comparator<Student>() { @Override
public int compare(Student s1, Student s2) {
// 升序
//return s1.getAge()-s2.getAge();
return s1.getAge().compareTo(s2.getAge());
// 降序
// return s2.getAge()-s1.getAge();
// return s2.getAge().compareTo(s1.getAge());
}
});
// 输出结果注: 还可以使用lambda表达式简化代码, 前提是JDK8开发环境, 如下:
List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30));
add(new Student("李四", 20));
add(new Student("王五", 60));
}
};
//对users按年龄进行排序
Collections.sort(stus, (s1,s2)->(s1.getAge()-s2.getAge()));

  

多条件排序:

		List<Student> stus = new ArrayList<Student>(){
{
add(new Student("张三", 30, 1));
add(new Student("李四", 20, 2));
add(new Student("王五", 40, 3));
add(new Student("赵六", 30, 4));
add(new Student("陈七", 40, 5));
add(new Student("周八", 20, 6));
}
};
Collections.sort(stus,new Comparator<Student>() { @Override
public int compare(Student s1, Student s2) {
int flag;
// 首选按年龄升序排序
flag = s1.getAge()-s2.getAge();
if(flag==0){
// 再按学号升序排序
flag = s1.getNum()-s2.getNum();
}
return flag;
}
}); System.out.println("年龄 学号 姓名 ");
for(Student s : stus){
System.out.println(s.getAge()+" "+s.getNum()+" "+s.getName());
}

  

自定义条件排序

		String[] order = {"语文","数学","英语","物理","化学","生物","政治","历史","地理","总分"};
final List<String> definedOrder = Arrays.asList(order);
List<String> list = new ArrayList<String>(){
{
add("总分");
add("英语");
add("政治");
add("总分");
add("数学");
}
};
Collections.sort(list,new Comparator<String>() { @Override
public int compare(String o1, String o2) {
int io1 = definedOrder .indexOf(o1);
int io2 = definedOrder .indexOf(o2);
return io1-io2;
}
}); for(String s:list){
System.out.print(s+" ");
}

  

使用lambda表达式简化代码:

Collections.sort(list, (o1, o2)->(definedOrder .indexOf(o1)-definedOrder .indexOf(o2)));
												

Comparator接口实现排序的更多相关文章

  1. Java基础 TreeSet()来实现数组的【定制排序】 : Comparable接口(自然排序) 或者 Comparator接口 (定制排序)

    笔记: //排序真麻烦!没有C++里的好用又方便!ORZ!ORZ!数组排序还还自己写个TreeSet()和( Comparable接口(自然排序) 或者 Comparator接口 (定制排序))imp ...

  2. TreeSet实现Comparator接口的排序算法的分析

    为了方便,用lambda表达式代替comparator接口 例子如下: public static void main(String[] args) { TreeSet<Integer> ...

  3. java中Comparatable接口和Comparator接口的区别

    1.不同类型的排序规则 .自然排序是什么?   自然排序是一种升序排序.对于不同的数据类型,升序规则不一样:   BigDecimal BigInteger Byte Double Float Int ...

  4. 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()

    在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...

  5. Comparatable接口和Comparator接口的使用与区别

    这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...

  6. Comparable接口与Comparator接口的比较————总结

    之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...

  7. JAVA排序(二) Comparator接口

    接着说关于Comparator接口, java.util Interface Comparator<T>(该泛型指定的是被比较的类),使用该接口不需要在待比较类进行比较操作,即在不修改源码 ...

  8. Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序

    package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...

  9. Java之——利用Comparator接口对多个排序条件进行处理

    转载自:http://blog.csdn.net/l1028386804/article/details/56513205 膜拜大神··· 一.需求 假设现在有个如此的需求:需要对一个这样的雇员列表进 ...

随机推荐

  1. Sqlserver on linux 高可用集群搭建

    一.环境准备 1 部署环境: 服务器数量:3台 Ip地址:192.168.1.191(主) 192.168.1.192(从) 192.168.1.193(从) 操作系统:CentOS Linux re ...

  2. Educational Codeforces Round 13 D. Iterated Linear Function 逆元+公式+费马小定理

    D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. jQuery系列(七):导航栏实例

    上代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. bzoj2733永无乡

    永无乡 HYSBZ - 2733 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接, ...

  5. Codeforces 645E. Intellectual Inquiry(DP,贪心)

    Codeforces 645E. Intellectual Inquiry 题意:给定一串字符,由前k个小写拉丁字母组成,要求在该字符串后面补上n个字符(也从前k个小写拉丁字母里面选),使得最后得到的 ...

  6. javaweb和数据库的简易商城系统

    这是一个基于Javaweb和数据库的简易商城系统.为大二夏季小学期完成. 目录结构 主要功能截图为: 一.购买用户 1.首页(除此界面其余界面访问需要登录才能进入) 查看商品 添加购物车 查看购物车 ...

  7. synchronized对象解析

    package com.haiyisoft.hyoaPc; public class Test7 { public static void main(String[] args) throws Int ...

  8. redis4. dict字典

    基础数据结构: (注意dict是字典,dict->type是相关函数指针, dict->type->keyDup是执行该方法) 具体调用链路: 渐进式rehash: 新增/删除时: ...

  9. Vue + Webpack-simple 怎么修改生产环境下运行的端口?

    开发环境下运行 npm run dev,默认运行在localhost:8080端口,想要修改端口,于是在"dev“后增加了--port 8081

  10. AnimationUtil

    import android.view.View; import android.view.animation.AlphaAnimation; public class AnimationUtil { ...