I want to sort an array which has two columns using a comparator on the second column and reverse the order.
The array is:
String[][] names = {{"a", "1234"}, {"b", "12312"}, {"c", "43"}};
And to return the sorted array I use:
String[][] out = Arrays.stream(names)
.sorted(
Comparator.comparing(x -> x[1])
)
.toArray(String[][]::new);
Which works well, but when I try to reverese the order of sort by using reversed()
:
String[][] out = Arrays.stream(names)
.sorted(
Comparator.comparing(x -> x[1]).reversed()
)
.toArray(String[][]::new);
I get error:
Error:(44, 66) java: array required, but java.lang.Object found
And my IDE highlights the x in the x[1]
part. What I'm doing wrong?