How to use a Java8 lambda to sort a stream in reverse order?

后端 未结 12 1674
暗喜
暗喜 2020-11-29 17:25

I\'m using java lambda to sort a list.

how can I sort it in a reverse way?

I saw this post, but I want to use java 8 lambda.

Here is my code (I used

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 18:11

    You can adapt the solution you linked in How to sort ArrayList in Java in decreasing order? by wrapping it in a lambda:

    .sorted((f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified())

    note that f2 is the first argument of Long.compare, not the second, so the result will be reversed.

提交回复
热议问题