java8 stream sort排序

集合对象为订单对象,主键、订单号、订单类型、总金额

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());

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值