I am trying to sort an array of Strings by reverse order (ignoring case), without modifying it, and just printing it. So I am using Java8 stream. But I can't manage to do it.
Here is my attempt :
package experimentations.chapter02;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class StringStream {
public static void main(String[] args) {
sortStrings();
}
public static void sortStrings(){
String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
System.out.println(
Arrays.stream(stringsArray)
.sorted(Comparator.comparing(String::toLowerCase).reversed())
.collect(Collectors.toList())
);
}
}
The problem here is that String::toLowerCase
is not accepted in the static method Comparator.comparing
.
Meanwhile, I managed to sort the array, but modifying it:
public static void sortStrings(){
String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
System.out.println(
Arrays.stream(stringsArray)
.map(String::toLowerCase)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList())
);
}
So, what is the simpliest workaround?