如何在java8中按多个属性对对象列表进行排序

如何在java8中按多个属性对对象列表进行排序,java,java-8,Java,Java 8,我知道这不是一个新问题,但我想按java8中首选的列对CSV文件进行排序 CSV的数据结构是List只需使用comparator,这是一个先按名字再按姓氏排序的示例 list.forEach(l -> l.sort(Comparator.comparing(CsvEntity::getFirstName) .thenComparing(CsvEntity::getLastName))); 当然,字段必须有getter才能使用方

我知道这不是一个新问题,但我想按java8中首选的列对CSV文件进行排序


CSV的数据结构是
List只需使用comparator,这是一个先按名字再按姓氏排序的示例

list.forEach(l -> l.sort(Comparator.comparing(CsvEntity::getFirstName)
                               .thenComparing(CsvEntity::getLastName)));
当然,字段必须有getter才能使用方法引用

最终函数byfirstName=csventy->csventy.getfirstname();
        final Function<CsvEntity, String> byfirstName = CsvEntity -> CsvEntity.getfirstname();
        final Function<CsvEntity, String> bylastname = CsvEntity -> CsvEntity.getlastname();
        final Function<CsvEntity, String> byaddress = CsvEntity -> CsvEntity.getaddress(); 
        System.out.println("Sorted in ascending order by Firstname and Lastname: ");
        List<CsvEntity> sortedlist =   CsvEntity.stream()
            .sorted(Comparator.comparing(byfirstName).thenComparing(bylastname))
            .collect(Collectors.toList());
最终函数bylastname=csventy->csventy.getlastname(); 最终函数byaddress=csventy->csventy.getaddress(); System.out.println(“按名字和姓氏升序排序:”); List sortedlist=CsvEntity.stream() .sorted(Comparator.comparing(按名字)。然后comparing(按名字)) .collect(Collectors.toList());
您想排序什么?最外层列表中的元素?或嵌套列表中的
csventy
元素?这将有助于:请参阅:
        final Function<CsvEntity, String> byfirstName = CsvEntity -> CsvEntity.getfirstname();
        final Function<CsvEntity, String> bylastname = CsvEntity -> CsvEntity.getlastname();
        final Function<CsvEntity, String> byaddress = CsvEntity -> CsvEntity.getaddress(); 
        System.out.println("Sorted in ascending order by Firstname and Lastname: ");
        List<CsvEntity> sortedlist =   CsvEntity.stream()
            .sorted(Comparator.comparing(byfirstName).thenComparing(bylastname))
            .collect(Collectors.toList());