1

I have a database table that I want to filter and then sort in reversed (descending) order. How do I express that in a Speedment stream similar to this:

films.stream()
    .filter(Film.LENGTH.greaterThan(120))
    .sorted(... some expression ...)
    .skip(100)
    .limit(50)
    .collect(Collectors.toList());

I want my SQL query to be optimized by Speedment and therefore I cannot use an anonymous lambda.

  • As opposed to general anonymous lambda ```Comparator::reversed``` it is significant that the built-in comparators are used or else Speedment will not be able to render a fully optimized SQL query in the background. – Per-Åke Minborg May 05 '17 at 17:15
  • 1
    If you change the question after I flag it, you can't complain about the flag.... – Robin Topper May 05 '17 at 17:17

1 Answers1

3

Use the built-in comparator for the field you want to use and apply the Comparator::reversed operation like this:

films.stream()
    .filter(Film.LENGTH.greaterThan(120))
    .sorted(Film.LENGTH.comparator().reversed())  // <--- Here
    .skip(100)
    .limit(50)
    .collect(Collectors.toList());
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140