Search code examples
javajava-8speedment

How do I sort a Stream in reversed order with Speedment


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.


Solution

  • 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());