list.stream.sorted对list按对象属性多级排序

list.stream.sorted对list按对象属性多级排序

一、列表多级排序:升序(默认)

// 对列表进行多级排序--升序  Integer类型,string类型,long类型
        List<Custom> sortedList = customObjectList.stream()
                .sorted(Comparator.comparingInt(Custom::getPrice)
                        .thenComparing(Custom::getOrgName)
                        .thenComparing(Custom::getOrgPath)
                        .thenComparing(Custom::getDeviceName)
                        .thenComparing(Custom::getPointName)
                        .thenComparingLong(Custom::getCreateTime))
                .collect(Collectors.toList());

二、列表多级排序:降序

   实现方式  末尾加一个 .reversed()
 //实现多级排序:同时对 Integer Long String类型的降序排列
        //Comparator比较器的顺序就是你想要的顺序
        List<Custom> sortedList2 = customObjectList.stream()
                .sorted(Comparator.comparingInt(Custom::getPrice)
                        .thenComparing(Custom::getOrgName)
                        .thenComparing(Custom::getOrgPath)
                        .thenComparing(Custom::getDeviceName)
                        .thenComparing(Custom::getPointName)
                        .thenComparingLong(Custom::getCreateTime)
                        .reversed())
                .collect(Collectors.toList());

三、分析代码

作用:对customObjectList列表进行多级排序。

1.stream()方法将customObjectList列表转换为一个Stream对象,以便使用Stream API进行操作。

2.sorted()方法,多级排序。

3.每个级别的排序,使用Comparator创建比较器,按属性排序。
整数,使用comparingInt()方法;
字符串,使用comparing()方法;
长整数,使用comparingLong()方法。

4.thenComparing() 将多个比较器组合。在每个级别的排序后,如果前一个级别的排序结果相同,则继续按照下一个级别的属性进行排序。

5.collect(Collectors.toList())方法返回排序后的列表。

四、完整的测试代码

package com.example.demo.other;

import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class Custom {
    private int id;
    private int price;
    private String orgName;
    private String orgPath;
    private String deviceName;
    private String pointName;
    private long createTime;

    public Custom(int id, int price, String orgName, String orgPath, String deviceName, String pointName, long createTime) {
        this.id = id;
        this.orgName = orgName;
        this.orgPath = orgPath;
        this.deviceName = deviceName;
        this.pointName = pointName;
        this.createTime = createTime;
        this.price = price;
    }

    @Override
    public String toString() {
        return "CustomObject{" +
                "id=" + id + '\'' +
                "price=" + price + '\'' +
                ", orgName='" + orgName + '\'' +
                ", orgPath='" + orgPath + '\'' +
                ", deviceName='" + deviceName + '\'' +
                ", pointName='" + pointName + '\'' +
                ", createTime=" + createTime +
                '}';
    }

    public static void main(String[] args) {
        List<Custom> customObjectList = new ArrayList<>();
        customObjectList.add(new Custom(0, 96, "Org1", "Path1", "Device1", "Point1", 1631840409));
        customObjectList.add(new Custom(1, 96, "Org1", "Path1", "Device1", "Point1", 1631840408));
        customObjectList.add(new Custom(2, 96, "Org1", "Path1", "Device1", "Point1", 1631840407));
        customObjectList.add(new Custom(3, 96, "Org1", "Path1", "Device1", "Point3", 1631840600));
        customObjectList.add(new Custom(4, 96, "Org1", "Path1", "Device1", "Point4", 1631840700));
        customObjectList.add(new Custom(5, 96, "Org1", "Path1", "Device5", "Point5", 1631840800));
        customObjectList.add(new Custom(6, 96, "Org1", "Path1", "Device6", "Point6", 1631840400));
        customObjectList.add(new Custom(7, 96, "Org7", "Path7", "Device7", "Point7", 1631840500));
        customObjectList.add(new Custom(8, 96, "Org8", "Path8", "Device8", "Point8", 1631840600));
        customObjectList.add(new Custom(9, 97, "Org9", "Path9", "Device9", "Point9", 1631840700));
        customObjectList.add(new Custom(10, 98, "Org10", "Path10", "Device10", "Point10", 1631840800));

        orderListASC(customObjectList);
        orderListDESC(customObjectList);

    }

    /**
     * 对列表进行多级排序---升序(默认)
     * 支持所有数据类型  :
     * String,
     * Integer,Long等数字类型。float,double
     */
    public static void orderListASC(List<Custom> customObjectList) {
        // 对列表进行多级排序--升序  Integer类型,string类型,long类型
        List<Custom> sortedList = customObjectList.stream()
                .sorted(Comparator.comparingInt(Custom::getPrice)
                        .thenComparing(Custom::getOrgName)
                        .thenComparing(Custom::getOrgPath)
                        .thenComparing(Custom::getDeviceName)
                        .thenComparing(Custom::getPointName)
                        .thenComparingLong(Custom::getCreateTime))
                .collect(Collectors.toList());

        System.out.println("打印排序后的结果---升序-----");
        for (Custom obj : sortedList) {
            System.out.println(obj.toString());
        }
    }

    /**
     * 对列表进行多级排序---降序
     * *    支持所有数据类型
     * 实现方式  末尾加一个 .reversed()
     */
    public static void orderListDESC(List<Custom> customObjectList) {

        /* 实现多级排序:同时对 Integer Long类型的降序排列
        List<Custom> sortedList2 = customObjectList.stream()
                .sorted(Comparator.comparingInt(Custom::getPrice)
                        .thenComparingLong(Custom::getCreateTime)
                        .reversed())
                .collect(Collectors.toList());
         */

        /* 实现多级排序:同时对 String类型的降序排列
         List<Custom> sortedList2 = customObjectList.stream()
                .sorted(Comparator.comparing(Custom::getOrgName)
                        .thenComparing(Custom::getOrgPath)
                        .thenComparing(Custom::getDeviceName)
                        .thenComparing(Custom::getPointName)
                        .reversed())
                .collect(Collectors.toList());
         */

        //实现多级排序:同时对 Integer Long String类型的降序排列
        //Comparator比较器的顺序就是你想要的顺序
        List<Custom> sortedList2 = customObjectList.stream()
                .sorted(Comparator.comparingInt(Custom::getPrice)
                        .thenComparing(Custom::getOrgName)
                        .thenComparing(Custom::getOrgPath)
                        .thenComparing(Custom::getDeviceName)
                        .thenComparing(Custom::getPointName)
                        .thenComparingLong(Custom::getCreateTime)
                        .reversed())
                .collect(Collectors.toList());

        System.out.println("打印排序后的结果---降序-----");
        for (Custom obj : sortedList2) {
            System.out.println(obj.toString());
        }
    }
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值