尝试使用Comparator按名称排序,忽略大小写以及首先为null [英] Attempting to use Comparator to sort by name, ignore case, as well as nulls first
问题描述
使用Java 8 Comparator类对项目列表进行排序时遇到问题。
I am having issues using the Java 8 Comparator class to sort a list of items.
我当前的工作比较器如下:
My current working comparator is below:
comparator = Comparator.comparing(Person::getName, Comparator.nullsFirst(Comparator.naturalOrder()));
此方法有效:它首先按名称对列表进行排序,并先使用空值。但是,我现在试图忽略名称的大小写。
This works: it orders the list by name with the null values first. However, I am now attempting to ignore case of the names.
我知道我可以编写一个新的getter来将名称全部小写,但是我不想
I know that I can write a new getter that returns the name all lowercase, but I do not want to go with this approach as I have to do this for multiple attributes.
在线查找,看来我应该使用 String.CASE_INSENSITIVE_ORDER
,但是我看到的唯一示例不包括空排序规范。
Looking online, it looks like I should be using String.CASE_INSENSITIVE_ORDER
, but the only examples I see do not include the null ordering specification.
我可以做这样的事情:
comparator = Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER);
但是,每当我尝试包含 Comparator.nullsFirst
我最终遇到类型错误,并对如何继续感到困惑。
However, whenever I try to include the Comparator.nullsFirst
I end up getting type errors, and am confused on how to continue.
我尝试做类似于
thenComparing(Comparator.nullsFirst(Comparator.naturalOrder))
但这还是行不通的。
有人可以向我提供一些建议,让我知道如何将它们链接在一起以按名称排序(不区分大小写) ),然后对空值进行排序。我似乎对类型感到困惑。
Could someone lend me some advice on how I can chain these together to sort by name (not case sensitive) and then order the nulls. I seem to be confusing myself with the types.
推荐答案
null $有两种类型c $ c> s您可以在列表中。您可以具有
null
Person
引用,并且可以具有 Person
s为 null
名称。
There are two types of null
s you can have in your list. You can have null
Person
references, and you can have Person
s with null
names.
在第一种情况下,您必须应用 nullsFirst
到要使用的基本比较器:
In the first case, you have to apply nullsFirst
to the base comparator you want to use:
comparator = Comparator.nullsFirst(
Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER));
如果您可以使用 null
名称,则需要确保您的密钥永远不会返回 null
,或者将 nullsFirst
应用于 String.CASE_INSENSITIVE_ORDER
。第二种选择当然容易得多:
If you have the possibility of null
names, you need to make sure that your key never returns a null
, or that you apply nullsFirst
to String.CASE_INSENSITIVE_ORDER
. The second option is of course much easier:
comparator = Comparator.comparing(
Person::getName, Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER));
如果您同时具有两种选择( null
引用和 null
名称),则必须将两个版本组合在一起,并两次应用 nullsFirst
:
If you have both options (null
references and null
names), you will have to combine both versions and apply nullsFirst
twice:
comparator = Comparator.nullsFirst(
Comparator.comparing(
Person::getName,
Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER)
)
);
如果要像这样链接多个比较,则外部 nullsFirst
可确保对 null
Person
进行正确排序,可将其应用于整个链: / p>
If you are chaining multiple comparisons like this, the outer nullsFirst
, which ensures that null
Person
s get sorted properly, can be applied to the entire chain:
comparator = Comparator.nullsFirst(
Comparator.comparing(
Person::getName,
Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER)
).thenComparing(...)
);
这篇关于尝试使用Comparator按名称排序,忽略大小写以及首先为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!