I have to do an excercise where i have to write a method orderQuestionsByLength(), which should sort questions by its length (descending)
the following code works (but it's not sorted descending):
`public void orderQuestionsByLength(){
Collections.sort(questions,Comparator.comparing(question -> question.getText().length()));
for (Question question : questions){
System.out.println(question);
}
}`
as soon as i add .reversed() my IDE throws an error Cannot resolve method 'getText' in 'Object' even though i have a method getText() and it worked before adding .reversed()
it drives me crazy, bc i don't know how to solve the problem, gpt says my code is right (not saying that i should rely on gpt, but i have another method where i sort integers descending, where i used reversed() without any problems
This can sometimes happen because the Comparator.comparing method can't correctly infer the type of the lambda parameter when it's chained with .reversed().
To solve this issue, you can provide explicit type information in the lambda expression. Here's how you can modify your
orderQuestionsByLengthmethod with(String question) -> ….