package com.example.demo.qqqq;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class TestArraySort {
public static void main(String[] args) {
List<person> list = new ArrayList<>();
list.add(new person(1,"2021-09-06"));
list.add(new person(2,"2021-09-08"));
list.add(new person(2,"2021-09-03"));
list.add(new person(3,"2021-09-07"));
list.add(new person(4,"2021-09-05"));
List newList = list.stream().sorted((o1, o2) -> {
int num = Integer.compare(o2.getAge(),o1.getAge());
if (num == 0) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d1 = format.parse(o1.getBirth());
Date d2 = format.parse(o2.getBirth());
return Long.compare(d2.getTime(), d1.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
return num;
}).collect(Collectors.toList());
newList.forEach(e->{
System.out.println(e.toString());
});
}
private static class person{
private int age;
private String birth;
@Override
public String toString() {
return "person{" +
"age=" + age +
", birth='" + birth + '\'' +
'}';
}
public person(int age, String birth) {
this.age = age;
this.birth = birth;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
}
}
list根据多个属性时间字符串排序
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:P2127 序列排序
下一篇:二叉树排序
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
排序算法之计数排序的优化
排序算法之计数排序的优化
数组 计数排序 最小值 -
Java stream用时间排序 java list map stream 排序
继续上次的话题,昨天带着大家伙看了 Lambda表达式,是不是感觉真香,哈哈哈,今天这个绝对是更香的存在。流是什么鬼东西不知道大家眼中的流是什么,大家知道官方是怎么说的么?流是Java API的新成员,它允许你以声明性方式处理数据集合(通过查询语句来表达,而不是临时编写一个实现)。就现在来说,你可以把它们看成遍历数据集的高级迭代器。。此外,流还可以透明地并行处理,你无需写任何多线程代码了!现在先带
Java stream用时间排序 java8 lambda map排序 stream filter List 数据处理