How do I sort string values in case-insensitive order in Java 8

  • A+
Category:Languages

How do I sort string values in case-insensitive order in the following?


List<Employee> listofEmployees = Arrays.asList(
new Employee(1, "aaa", Arrays.asList(123, 345, 678)),
new Employee(1, "bbb", Arrays.asList(91011, 121314, 1516117)),
new Employee(2, "ccc", Arrays.asList(181920, 212223, 242526)),
new Employee(3, "ddd", Arrays.asList(272829, 303132, 333435)),
new Employee(4, "BBB", Arrays.asList(29, 332, 33))
);

I wrote like this:


listofEmployees.stream().sorted(Comparator.comparing(Employee::getName).reversed())
.forEach(s -> System.out.println(s.getName()));

How do I pass a string case insensitive option here?


Try this

Comparator.comparing(Employee::getName, String.CASE_INSENSITIVE_ORDER) 

Comment

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