7

I have this list that I want to order in reserve order, but I didn't find any .reversed() function in autocomplete assist

 myMenus(user)
                .stream()
                .filter(mps ->  mps.get1PercentageChange() > 0 &&
                                mps.get2PercentageChange() > 0 &&
                                mps.get3PercentageChange() > 0 &
                                mps.get4PercentageChange() > 0)
                .sorted(comparing(mps -> mps.getDailyPercentageChange()))
                .collect(toList());

I have also tried:

myMenus(user)
        .stream()
        .filter(mps ->  mps.get1PercentageChange() > 0 &&
                        mps.get2PercentageChange() > 0 &&
                        mps.get3PercentageChange() > 0 &
                        mps.get4PercentageChange() > 0)
        .sorted(comparing(mps -> mps.getDailyPercentageChange()).reversed())
        .collect(toList());

but then I have the compilation error:

Cannot infer type argument(s) for <T, U> comparing(Function<? super T,? 
     extends U>)
1
  • How about just using comparing(mps -> -mps.getDailyPercentageChange())? Commented Jun 1, 2018 at 14:43

2 Answers 2

8

It's a type inference issue. You'll need to help the compiler out.

few things you could try:

.sorted(comparing(T::getDailyPercentageChange).reversed())

or

.sorted(comparing((T mps) -> mps.getDailyPercentageChange()).reversed())

Where T is the type of elements being compared.

Sign up to request clarification or add additional context in comments.

Comments

5

Comparators have a reversed method to get the reverse ordering, so :

.sorted(comparing(mps -> mps.getDailyPercentageChange()).reversed())

should work.

2 Comments

How about using a method reference instead: .sorted(comparing(Mps::getDailyPercentageChange).reversed()) (where Mps is the type of mps)? That would be a bit more readable.
Depending on the class name length, a lambda can sometimes be conciser. I also presume that Mps is not the actual class name but an acronym.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.