Using Comparator.comparing to sort Java Stream
- JP

- Jun 25, 2023
- 2 min read
Updated: Feb 5, 2024

Introduction
Sorting data is a common task in many software development projects. When working with collections of objects in Java, a powerful and flexible approach to sorting is to use the Comparator.comparing interface in conjunction with Streams. In this post, we are going to show that using Comparator.comparing to sort Java Stream can make sorting elegant and efficient.
What is the Comparator.comparing interface?
The Comparator.comparing interface is a feature introduced in Java 8 as part of the java.util.Comparator. package. It provides a static method called comparing that allows you to specify a key function (sort key) to compare objects. This function is used to extract a value from an object and compare it against that value during sorting.
Flexibility in sorting with Comparator.comparing
One of the main advantages of the Comparator.comparing interface is its flexibility. With it, we can perform sorting in different fields of an object, allowing the creation of complex sorting logic in a simple and concise way. Notice in the code below that simply in the sorted() method, we pass the Comparator.comparing interface as an argument, which in turn, passes the city field as an argument using method reference (People::getCity) performing the sort by this field.
Output
Monica
John
Mary
Anthony
Seth
Multi-criteria ordering
Often, it is necessary to perform sorting based on multiple criteria. This is easily achieved with the Comparator.comparing. interface by simply chaining together several comparing methods, each specifying a different criterion. Java will carry out the ordering according to the specified sequence. For example, we can sort the same list by city and then by name: Comparator.comparing(People::getCity).thenComparing(People:: getName).
Ascending and descending sort
Another important advantage of the Comparator.comparing interface is the ability to perform sorting in both ascending and descending order. To do this, just chain the method reversed() as in the code below:
Output
Seth
Mary
John
Anthony
Monica
Efficiency and simplicity
By using the Comparator.comparing interface in conjunction with Streams, sorting becomes more efficient and elegant. The combination of these features allows you to write clean code that is easy to read and maintain. Furthermore, Java internally optimizes sorting using efficient algorithms, resulting in satisfactory performance even for large datasets.
Final conclusion
The Comparator.comparing interface is a powerful tool to perform the sorting of Streams in Java. Its flexibility, ascending and descending sorting capabilities, support for multiple criteria, and efficient execution make it a valuable choice for any Java developer. By taking advantage of this interface, we can obtain a more concise, less verbose and efficient code, facilitating the manipulation of objects in a Stream.
Hope you enjoyed!



Read this Backlink 1 Backlink 2 Backlink 3 Backlink 4 Backlink 5 Backlink 6 Backlink 7 Backlink 8 Backlink 9 Backlink 10.
Follow this important information: Info 1 Info 2 Info 3 Info 4 Info 5 Info 6 Info 7 Info 8 Info 9 Info 10 Info 11 Info 12 Info 13 Info 14 Info 15 Info 16 Info 17.
Read this Info 1 Info 2 Info 3 Info 4 Info 5 Info 6 Info 7 Info 8 Info 9 Info 10 Info 11 Info 12.
Important information recommendations that you should read: Info 1 Info 2 Info 3 Info 4 Info 5 Info 6 Info 7 Info 8 Info 9 Info 10 Info 11 Info 12 Info 13 Info 14 Info 15 Info 16 Info 17 Info 18 Info 19 Info 20 & Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 Topic 6 Topic 7 Topic 8 Topic 9 Topic 10 Topic 11 Topic 12 Topic 13 Topic 14 Topic 15 Topic 16 Topic 17 Topic 18 Topic 19 Topic 20.
I recommend that you read some of this information so that it can be used as a reference in writing articles: Info 1 Info 2 Info 3 Info 4 Info 5 Info 6 Info 7 Info 8 Info 9 Info 10 Info 11 Info 12 Info 13 Info 14 Info 15 Info 16 Info 17 Info 18 Info 19 Info 20 Info 21 Info 22 Info 23 Info 24 Info 25 And this Link 1 Link 2 Link 3 Link 4 Link 5 Link 6 Link 7 Link 8 Link 9 Link 10 Link 11 Link 12 Link 13 Link 14 Link 15 Link 16 Link 17 Link 18 Link 19 Link 20 Link 21 Link 22 Link 23 Link 24 Link 25 Then this Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Page 12 Page 13 Page 14 Page 15 Page 16 Page 17 Page 18 Page 19 Page 20 Page 21 Page 22 Page 23 Page 24 Page 25 Next this Post 1 Post 2 Post 3 Post 4 Post 5 Post 6 Post 7 Post 8 Post 9 Post 10 Post 11 Post 12 Post 13 Post 14 Post 15 Post 16 Post 17 Post…