Languages
[Edit]
EN

Java 8 - sort list with stream.sorted()

0 points
Created by:
Zachariah
298

In this article, we would like to show you how to sort lists with stream.sorted() in Java 8.

Quick solution

Edit

Natural order:

Reverse order:

 

1. Natural order

Edit

In this example, we use stream.sorted() method to sort the list in alphabetical order and save the result as a new list - sorted.

Output:

Note:

By default, the sorted() method uses Comparator.naturalOrder().

2. Reverse order

Edit

In this example, we use stream.sorted() method with Comparator.reverseOrder() to sort the list in reverse order and save the result as a new list - sorted.

Output:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join