集合对象为订单对象,主键、订单号、订单类型、总金额
public class OrderInfo {
private Integer id;
private String orderNo;
private Integer type;
private Double total;
public OrderInfo(Integer id,String orderNo,Integer type,Double total){
this.id = id;
this.orderNo = orderNo;
this.type = type;
this.total = total;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Double getTotal() {
return total;
}
public void setTotal(Double total) {
this.total = total;
}
}
添加测试数据:
List<OrderInfo> list = new ArrayList<OrderInfo>(8);
list.add(new OrderInfo(1,"T20201116401",1,100.89d));
list.add(new OrderInfo(2,"T20201116402",1,200.89d));
list.add(new OrderInfo(4,"T20201116404",2,180.89d));
list.add(new OrderInfo(5,"T20201116405",2,90.89d));
list.add(new OrderInfo(3,"T20201116403",3,170.89d));
排序:
1.按照ID进行排序
List<OrderInfo> orderList = list.stream().sorted(Comparator.comparing(OrderInfo::getId)).collect(Collectors.toList());
2.按照ID进行倒序
List<OrderInfo> orderList2 = list.stream().sorted(Comparator.comparing(OrderInfo::getId).reversed()).collect(Collectors.toList());
3.先按照订单类型排序,在按照订单总金额排序
List<OrderInfo> orderList3 = list.stream().sorted(Comparator.comparing(OrderInfo::getType)
.thenComparing(OrderInfo::getTotal))
.collect(Collectors.toList());