Java Comparator.nullsLast和Comparator.nullsFirst不按预期工作。

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

Java Comparator.nullsLast and Comparator.nullsFirst not working as expected

问题

以下是您提供的内容的中文翻译:

我有以下方法,在尝试按列表中为null的字段进行排序时,在sort()中抛出NPE:

private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
        final String[] split,
        final Comparator<MobileSearch> comparing) {

    final var direction = getDirection(split);

    final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());

    final var comparator = direction.equals(Constants.ASC)
            ? comparing.thenComparing(Comparator.nullsLast(comparing))
            : comparing.thenComparing(Comparator.nullsFirst(comparing.reversed()));

    modifiableList.sort(comparator);
    return modifiableList;
}

作为参数传递了一个比较器,该比较器使用了几个keyExtractors。

您可以在下面看到一些方法调用:

final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getMsisdn));

final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getAssignedProfile));

我想要的只是在每种情况下应用modifiableList.sort()时避免获取NPE,但是使用我的代码片段无法实现。有什么想法?

英文:

I have the following method, which throws NPE in sort() when I am trying to sort by a field which is null in the list :

private List&lt;MobileSearch&gt; sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
			final String[] split,
			final Comparator&lt;MobileSearch&gt; comparing) {

	final var direction = getDirection(split);

    final var modifiableList = new ArrayList&lt;MobileSearch&gt;(mobilesPostResponse.getMobiles());

	final var comparator = direction.equals(Constants.ASC)
			? comparing.thenComparing(Comparator.nullsLast(comparing))
			: comparing.thenComparing(Comparator.nullsFirst(comparing.reversed()));

	modifiableList.sort(comparator);
	return modifiableList;
}

A comparator is passed as a parameter which is using several keyExtractors.
You can see below some method calls :

final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getMsisdn));

final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getAssignedProfile));

All I want is to avoid getting NPE when applying the modifiableList.sort() in EVERY case, but with my piece of code I can not achieve it. Any ideas?

答案1

得分: 2

您正在使用comparing.thenComparing(...)对空值进行比较,然后才使用Comparators.nulls...(..)。您只需要使用Comparator.nulls...(..),如下所示:

private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
        final String[] split,
        final Comparator<MobileSearch> comparing) {

    final var direction = getDirection(split);

    final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());

    final var comparator = direction.equals(Constants.ASC)
            ? Comparator.nullsLast(comparing)
            : Comparator.nullsFirst(comparing.reversed());

    modifiableList.sort(comparator);
    return modifiableList;
}
英文:

You are using the comparing comparator on the null values by using comparing.thenComparing(...) and then only you use the Comparators.nulls...(..). You just need the Comparator.nulls...(..) like this :

private List&lt;MobileSearch&gt; sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
        final String[] split,
        final Comparator&lt;MobileSearch&gt; comparing) {

    final var direction = getDirection(split);

    final var modifiableList = new ArrayList&lt;MobileSearch&gt;(mobilesPostResponse.getMobiles());

    final var comparator = direction.equals(Constants.ASC)
            ? Comparator.nullsLast(comparing)
            : Comparator.nullsFirst(comparing.reversed());

    modifiableList.sort(comparator);
    return modifiableList;
}

huangapple
  • 本文由 发表于 2023年5月29日 19:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356942.html
匿名

发表评论

匿名网友

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

确定