java list集合排序按某一属性排序操作
作者:Wales_ 时间:2021-06-24 03:33:25
我就废话不多说了,大家还是直接看代码吧~
public List<FreightM> sortList(List<FreightM> list){
Collections.sort(list, new Comparator<FreightM>(){
@Override
public int compare(FreightM o1, FreightM o2) {
if(o1.getType()==0){
return -1;
}else{
return 1;
}
}
});
return list;
}
实现compareTo()方法,
返回值为-1,第一个参数排前面;返回值为1,则第二个参数排前面
补充知识:java8List.sort()排序功能
我就废话不多说了,大家还是直接看代码吧~
//按照List中对象的id属性升序
list.sort(Comparator.comparing(Stu::getId))
//按照List中对象的id属性降序
list.sort(Comparator.comparing(Stu::getId).reversed());
//多条件升序
list.sort(Comparator.comparing(Stu::getId).thenComparing(Stu::getSid));
//id升序,sid降序
list.sort(Comparator.comparing(Stu::getId).reversed().thenComparing(Stu::getSid));
//key值重复的map
MultiValueMap<Integer, String> timeMap = new LinkedMultiValueMap<>();
//集合升序排序
Collections.sort(student, new Comparator(){
public int compare(StudentVo p1, StudentVo p2) {
return Integer.parseInt(p1.getStudentCode()) - Integer.parseInt(p2.getStudentCode());
}
});
来源:https://blog.csdn.net/wanzhix/article/details/80682300
标签:java,list,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot异步调用方法并接收返回值
2023-08-16 22:33:49
![](https://img.aspxhome.com/file/2023/5/58055_0s.jpg)
Java深入探究Object类的方法
2022-11-24 06:48:53
![](https://img.aspxhome.com/file/2023/4/67014_0s.png)
简单聊一聊Java线程池ThreadPoolExecutor
2021-10-23 15:56:58
![](https://img.aspxhome.com/file/2023/3/64053_0s.jpg)
Mybatis是这样防止sql注入的
2022-05-30 02:05:16
Java文件操作之IO流 File类的使用详解
2023-07-26 00:49:41
Mybatis Plus select 实现只查询部分字段
2022-07-23 18:51:32
![](https://img.aspxhome.com/file/2023/9/61279_0s.jpg)
idea中的Maven导包失败问题解决方案汇总
2023-07-12 12:34:22
![](https://img.aspxhome.com/file/2023/7/70567_0s.png)
Android中通过RxJava进行响应式程序设计的入门指南
2023-06-27 08:17:46
maven引入本地jar包运行报错java.lang.NoClassDefFoundError解决
2022-10-15 01:42:24
Java如何实现字符串每隔4位加空格
2023-11-27 06:00:09
Spring的@Autowired加到接口上但获取的是实现类的问题
2023-08-23 21:32:21
轻松理解Java面试和开发中的IoC(控制反转)
2023-08-10 03:00:35
![](https://img.aspxhome.com/file/2023/4/58184_0s.jpg)
兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil
2023-11-03 05:35:06
简单记事本java源码实例
2023-11-26 02:03:17
![](https://img.aspxhome.com/file/2023/4/59744_0s.jpg)
Java语言实现简单FTP软件 FTP协议分析(1)
2023-11-10 17:57:21
![](https://img.aspxhome.com/file/2023/7/59027_0s.jpg)
Spring整合Quartz实现定时任务调度的方法
2023-07-07 00:55:55
详解Alibaba Java诊断工具Arthas查看Dubbo动态代理类
2021-08-04 03:16:28
![](https://img.aspxhome.com/file/2023/4/64064_0s.png)
详解SpringMVC常用注解功能及属性
2021-12-29 02:49:23
java并发编程_线程池的使用方法(详解)
2023-03-29 22:23:00
![](https://img.aspxhome.com/file/2023/1/69711_0s.png)
Eclipse+Java+Swing+Mysql实现工资管理系统
2023-07-26 08:51:05
![](https://img.aspxhome.com/file/2023/0/57960_0s.png)