0

Today I would like to sort my List of Movie by rating. I use sort method like below and it works fine till I would like to make a reversed order.

List<Movie> result = movieRepository.findAll();
result.sort(Comparator.comparing(m->m.getRating()));

That works perfect however when I try to do something like that:

 List<Movie> result = movieRepository.findAll();
 result.sort(Comparator.comparing(m->m.getRating()).reversed());

It shows me that:Cannot resolve method getRating(). So he can resolve it until I try to make it reversed order? What is going on here?

tomas
  • 15
  • 2

1 Answers1

0

AFAIK, there's some bug in compiler that they somehow can't guess the correct type when chaining Comparator.

In your case you can workaround by using method reference:

List<Movie> result = movieRepository.findAll();
result.sort(Comparator.comparing(Movie::getRating).reversed());
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51