I have a list of Strings with two characters and want to sort by first character ascending and second character descending, for example "S1", "T1", "T2" should be sorted to "S1", "T2", "T1".
Seemed simple enough:
strings.sort(Comparator.comparing(code -> code.substring(0,1))
.thenComparing(code -> code.substring(1,2).reversed()));
After typing out the first part
strings.sort(Comparator.comparing(code -> code.substring(0,1))
Anything is still okay, autocomplete tells me about substring (Intellij), but when I add
.thenComparing(code -> code.substring(1,2).reversed()));
Somehow Java doesn't know anymore that it's working with Strings and gives me the error
Cannot resolve method 'substring' in 'Object'
for the .substring method calls on both.
Same happens when using stream().sorted()