I have an ArrayList and want sort it in descending order. I use for it java.util.stream.Stream.sorted(Comparator)
method. Here is a description according Java API:
Returns a stream consisting of the elements of this stream, sorted according to the provided
Comparator
.
this methods return me a sort with ascending order. Which parameter should I change, just to have the descending order?
You can use
Comparator.reverseOrder()
to have a comparator that imposes the reverse of the natural ordering.If you want to reverse the ordering of an existing comparator, you can use
Comparator.reversed()
.Sample code:
Java 8
Comparator
interface has areversed
method : https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#reversed--