Java集合List按日期升序或降序四种方法
工作中常用到Java日期排序(升序或者降序),总结Java集合List按日期升序或降序常见的三种方法如下准备的类import lombok.AllArgsConstructor;import lombok.Data;@AllArgsConstructor@Dataclass Order {/*** 订单号*/private String orderNum;/*** 付款时间...
·
总结Java集合List按日期升序或降序常见的四种方法如下
四种方法快速预览
Method 1、不管是Date、String、Long类型的日期都可以排序,无需转换
orders.sort((t1, t2) -> t2.getPayTime().compareTo(t1.getPayTime()));
Method 2、利用stream,需要把字符串日期转换为时间戳
orders = orders.stream().sorted((t1,t2) ->
Long.compare(convertTimeToLong(t2.getPayTime()),
convertTimeToLong(t1.getPayTime()))).
collect(Collectors.toList());
Method 3、利用Comparator接口实现,java8的写法
Collections.sort(orders, Comparator.comparing(Order::getPayTime, (t1, t2) -> t2.compareTo(t1)));
Method 4、利用Comparator接口实现,java8之前通常这样写
Collections.sort(orders,new Comparator<Order>(){
@Override
public int compare(Order o1, Order o2) {
return o2.getPayTime().compareTo(o1.getPayTime());
}
});
准备的类
import lombok.AllArgsConstructor;
import lombok.Data;
@AllArgsConstructor
@Data
class Order {
/**
* 订单号
*/
private String orderNum;
/**
* 付款时间
*/
private String payTime;
/**
* 支付金额
*/
private Integer actualAmount;
}
方法一
import java.util.ArrayList;
import java.util.List;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//不管是Date、String、Long类型的日期都可以排序,无需转换
orders.sort((t1, t2) -> t2.getPayTime().compareTo(t1.getPayTime()));
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
方法二
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用stream,需要把字符串日期转换为时间戳
orders = orders.stream().sorted((t1,t2) ->
Long.compare(convertTimeToLong(t2.getPayTime()), convertTimeToLong(t1.getPayTime()))).
collect(Collectors.toList());
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
/**
* 将字符串转日期成Long类型的时间戳,格式为:yyyy-MM-dd HH:mm:ss
*/
public static Long convertTimeToLong(String time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse(time, formatter);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
}
方法三
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用Comparator接口实现,java8的写法
Collections.sort(orders, Comparator.comparing(Order::getPayTime, (t1, t2) -> t2.compareTo(t1)));
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
方法四
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
public class TestTimeSort {
public static void main(String[] args) {
Order order1 = new Order("1101", "2019-11-03 00:30:31", 100);
Order order2 = new Order("1102", "2019-11-03 00:30:32", 200);
Order order3 = new Order("1103", "2019-11-03 00:30:33", 300);
Order order4 = new Order("1104", "2019-11-03 00:30:34", 400);
Order order5 = new Order("1105", "2019-11-03 00:30:35", 500);
List<Order> orders = new ArrayList<>();
orders.add(order2);
orders.add(order1);
orders.add(order4);
orders.add(order3);
orders.add(order5);
System.out.println("------------排序前-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
//利用Comparator接口实现,java8之前通常这样写
Collections.sort(orders,new Comparator<Order>(){
@Override
public int compare(Order o1, Order o2) {
return o2.getPayTime().compareTo(o1.getPayTime());
}
});
System.out.println("------------倒序后-----------------");
for (Order order : orders) {
System.out.println("orderNum=" + order.getOrderNum() + ",payTime=" + order.getPayTime());
}
}
}
四种方法最后输出如下
------------排序前-----------------
orderNum=1102,payTime=2019-11-03 00:30:32
orderNum=1101,payTime=2019-11-03 00:30:31
orderNum=1104,payTime=2019-11-03 00:30:34
orderNum=1103,payTime=2019-11-03 00:30:33
orderNum=1105,payTime=2019-11-03 00:30:35
------------倒序后-----------------
orderNum=1105,payTime=2019-11-03 00:30:35
orderNum=1104,payTime=2019-11-03 00:30:34
orderNum=1103,payTime=2019-11-03 00:30:33
orderNum=1102,payTime=2019-11-03 00:30:32
orderNum=1101,payTime=2019-11-03 00:30:31
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献8条内容
所有评论(0)