php editor Strawberry brings you an interesting string sorting problem: sorting string lengths, but in reverse. That is, the longest string comes first. This problem can be solved by using built-in functions and custom sort functions. During the sorting process, we need to calculate the length of each string and sort it in order from largest to smallest length. Next, we will detail how to implement this interesting string sorting problem.
I had to do an exercise where I had to write a method orderquestionsbylength() which should sort the questions by their length (descending order)
The following code works (but does not sort in descending order):
`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 the error Cannot resolve method 'gettext' in 'object' Even though I have a method gettext() and it is adding .reversed It will work before()
This is driving me crazy because I don't know how to solve this problem and gpt says my code is correct (not that I should rely on gpt but I have another way to sort integers in descending order and I Returned() was used without any problems
Sometimes this happens because the comparator.comparing method cannot correctly infer the type of the lambda parameter when chained with .reversed().
To resolve this issue, you can provide explicit type information in the lambda expression. Here's how to modify the orderquestionsbylength
method using (string question) -> …
.
public void orderquestionsbylength() { collections.sort(questions, comparator.comparing((question question) -> question.gettext().length()).reversed()); for (question question : questions) { system.out.println(question); } }
call comparator#reversed
.
Pay attention to the grammar. Call reversed after
generating the comparator using comparator.comparing
.
comparator.comparing
is a static
method call that generates an instance (object). comparator#reversed
is an instance method call that generates another object. As pointed out in chf's answer , you may need to be explicit about the data type of the parameter passed to the lambda: (string issue) -> …
.
public void orderquestionsbylength() { collections.sort( questions, comparator .comparing( ( string question ) -> question.gettext().length() ) // static method call. .reversed() // instance method call. ); questions.foreach( system.out :: println ) ; }
BTW, you can collapse a for
loop using collections#foreach
and a method reference, as shown above.
Complete sample application code:
package work.basil.example; import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class Infer { public static void main ( String[] args ) { Comparator < Question > comparator = Comparator .comparing ( ( Question question ) -> question.text ( ).length ( ) ) .reversed ( ); ArrayList < Question > questions = new ArrayList <> ( List.of ( new Question ( "alpha" , 1 ) , new Question ( "b" , 2 ) , new Question ( "gamma" , 3 ) ) ); questions.sort ( comparator ); questions.forEach ( System.out :: println ); } } record Question( String text , int points ) { }
The above is the detailed content of Sort string lengths, but in reverse (longest string first). For more information, please follow other related articles on the PHP Chinese website!