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.
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.
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.
I can do something like this:
comparator = Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER);
However, whenever I try to include the Comparator.nullsFirst I end up getting type errors, and am confused on how to continue.
I've tried doing a chain similar to
thenComparing(Comparator.nullsFirst(Comparator.naturalOrder))
but that also doesn't work.
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.