【发布时间】:2023-01-06 23:12:35
【问题描述】:
我需要通过其对象属性之一的一个属性来比较一组对象。
我在做 :
List<Sell> collect = sells.stream()
.sorted(Comparator.comparing(Sell::getClient.name, String::compareToIgnoreCase))
.collect(Collectors.toList());
编译不了,有大神知道怎么弄吗?
谢谢。
【问题讨论】:
-
错误是什么? Shell.getClient 是什么样子的?
-
什么是销售、客户类定义?什么是您正在使用流的销售。提供完整代码。
-
如果您想要不区分大小写的
Comparator,请使用String.CASE_INSENSITIVE_ORDER。String::compareToIgnoreCase是一个返回 int 而不是Comparator的比较方法 -
它只是说找不到 getClient.name。在 Sell 对象中,我有一个具有属性名称的 Client 对象。我觉得我不能将此方法与嵌套对象的属性一起使用。