英文:
How to sort List with Comparator to sort null value properly?
问题
我想要实现的是对一个具有两个条件的列表进行排序,我希望基于对象内部的列表进行排序,当它为 null
时,我希望它排在最后。
以下是我的操作,这是我的 UnAssignedSoResponse.java
对象:
public class UnAssignedSoResponse {
public Double cbm;
public ShipmentAddressGroupingResponseHdOnlyDto shipmentAddressGroupingResponseHdOnlyDto;
}
这是我的服务中代码的样子:
List<UnAssignedSoResponse> unAssignedSoResponse = ObjectMapperUtils.mapAll(salesOrders, UnAssignedSoResponse.class);
unAssignedSoResponse.sort(Comparator.comparing(u -> u.getShipmentAddressGroupingResponseHdOnlyDto().getShipmentGroupName()), Comparator.nullsLast(Comparator.naturalOrder()));
在上面的代码中,变量 unAssignedSoResponse
是一个列表,我想要基于名为 shipmentAddressGroupingResponseHdOnlyDto
的列表进行排序,有时 shipmentAddressGroupingResponseHdOnlyDto
是 null
,我希望将其置于列表的末尾。但是,如果我像上面那样编写代码,sort
部分会被下划线标出红色,我会收到以下错误:
The method sort(Comparator<? super UnAssignedSoResponse>) in the type List<UnAssignedSoResponse> is not applicable for the arguments (Comparator<UnAssignedSoResponse>, Comparator.nullsLast(Comparator.naturalOrder()))
如果我想要基于 shipmentGroupName
进行排序,可以这样做:
unAssignedSoResponse.sort(Comparator.comparing(u -> u.getShipmentAddressGroupingResponseHdOnlyDto().getShipmentGroupName()));
那么如何在这种情况下进行安全的 null
排序呢?
更新:
根据 Louis Wasserman 的回答,我将我的代码更新为以下形式:
unAssignedSoResponse.sort(
Comparator.nullsLast(
Comparator.comparing(
u -> u.getShipmentAddressGroupingResponseHdOnlyDto(),
Comparator.nullsLast(Comparator.comparing(d -> d.getShipmentGroupName()))
)
)
);
虽然没有错误,但是我的列表不会将 null
排在最后,而且根本没有排序。我错过了什么?
英文:
What I trying to achieve is sorting a list with two conditions, I want to sort based List that inside my object and when it's null
I want it sorted at last.
Here is what I do, here is my UnAssignedSoResponse.java object :
public class UnAssignedSoResponse {
public Double cbm;
public ShipmentAddressGroupingResponseHdOnlyDto shipmentAddressGroupingResponseHdOnlyDto;
}
and here is how my code in the service look like :
List<UnAssignedSoResponse> unAssignedSoResponse = ObjectMapperUtils.mapAll(salesOrders, UnAssignedSoResponse.class);
unAssignedSoResponse.sort(Comparator.comparing(u -> u.getShipmentAddressGroupingResponseHdOnlyDto().getShipmentGroupName()),Comparator.nullsLast(Comparator.naturalOrder()));
above my variable unAssignedSoResponse
is a list, and I want it to sort based list that named shipmentAddressGroupingResponseHdOnlyDto
, sometimes shipmentAddressGroupingResponseHdOnlyDto
is null
, I want to set null at the last of the list. but I got an error in "sort" underlined red if I write something like above, the error is :
The method sort(Comparator<? super UnAssignedSoResponse>) in the type List<UnAssignedSoResponse> is not applicable for the arguments (Comparator<UnAssignedSoResponse>, Comparator.nullsLast(Comparator.naturalOrder()))
if i want to sort based the shipmentGroupName, i can do something like this :
unAssignedSoResponse.sort(Comparator.comparing(u -> u.getShipmentAddressGroupingResponseHdOnlyDto().getShipmentGroupName()));
How do I sort null
safe over there?
UPDATE :
i update my code as Louis Wasserman answer to something like this :
unAssignedSoResponse.sort(
Comparator.nullsLast(
Comparator.comparing(
u -> u.getShipmentAddressGroupingResponseHdOnlyDto(),
Comparator.nullsLast(Comparator.comparing(d -> d.getShipmentGroupName()))
)
)
);
there is no error, but my list won't sorted the null at last ArrayList and it won't sort at all, what did i missed?
答案1
得分: 3
unAssignedSoResponse.sort(
Comparator.nullsLast(
Comparator.comparing(
u -> u.getShipmentAddressGroupingResponseHdOnlyDto(),
Comparator.nullsLast(Comparator.comparing(d -> d.getShipmentGroupName()))));
你只需要使用 nullsLast
。
英文:
unAssignedSoResponse.sort(
Comparator.nullsLast(
Comparator.comparing(
u -> u.getShipmentAddressGroupingResponseHdOnlyDto(),
Comparator.nullsLast(Comparator.comparing(d -> d.getShipmentGroupName())));
All you need is to use nullsLast
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论