如何使用 Comparator 对 List 进行排序以正确排序空值?

huangapple go评论76阅读模式
英文:

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 的列表进行排序,有时 shipmentAddressGroupingResponseHdOnlyDtonull,我希望将其置于列表的末尾。但是,如果我像上面那样编写代码,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&lt;UnAssignedSoResponse&gt; unAssignedSoResponse = ObjectMapperUtils.mapAll(salesOrders, UnAssignedSoResponse.class);

    unAssignedSoResponse.sort(Comparator.comparing(u -&gt; 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&lt;? super UnAssignedSoResponse&gt;) in the type List&lt;UnAssignedSoResponse&gt; is not applicable for the arguments (Comparator&lt;UnAssignedSoResponse&gt;, Comparator.nullsLast(Comparator.naturalOrder()))

if i want to sort based the shipmentGroupName, i can do something like this :

        unAssignedSoResponse.sort(Comparator.comparing(u -&gt; 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 -&gt; u.getShipmentAddressGroupingResponseHdOnlyDto(),
                Comparator.nullsLast(Comparator.comparing(d -&gt; 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 -&gt; u.getShipmentAddressGroupingResponseHdOnlyDto(),
        Comparator.nullsLast(Comparator.comparing(d -&gt; d.getShipmentGroupName())));

All you need is to use nullsLast.

huangapple
  • 本文由 发表于 2020年10月1日 11:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64148706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定