如何通过多个属性在java8的对象列表列表中排序 [英] how sort in java8 list of lists of object by multiple properties

查看:1742
本文介绍了如何通过多个属性在java8的对象列表列表中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this is not a new question but I want to sort CSV file by Column preferred in java8.

CSV的数据结构为List<List<CsvEntity>,我需要首先按CsvEntity的一个字段进行排序,然后按其他(并非必须)示例进行排序:firstName然后按lastName.

The Data-structure of CSV is List<List<CsvEntity> I need to sort first by one field of the CsvEntity then by other (not must) example by: firstName then by lastName.

class CsvEntity{
   private String firstName
   private String lastName
   private String address
}

此之后-我需要进行哪些修改以支持Java8中对多个键的排序?

What modifications do I need to make for supporting sort on multiple keys in Java8?

推荐答案

只使用比较器,一个按firstName然后按lastName排序的示例

Just use comparator, an example of sorting by firstName then by lastName

list.forEach(l -> l.sort(Comparator.comparing(CsvEntity::getFirstName)
                               .thenComparing(CsvEntity::getLastName)));

当然,您必须为字段使用getter才能使用方法引用

Of course you have to have getters for your fields to use method reference

这篇关于如何通过多个属性在java8的对象列表列表中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆