top of page

Coffee and Tips Newsletter

Inscreva-se na nossa newsletter semanal

Nos vemos em breve!

Using Comparator.comparing to sort Java Stream

Writer: JP
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!



















24 Comments


johnniwill.ia.ms4069
Nov 16

88ibet chia nội dung theo mấy câu hỏi khá dễ theo dõi, kiểu có thắc mắc gì thì nhìn tiêu đề rồi đọc đúng đoạn đó thôi. Mình thích cách trình bày này hơn là một bài dài liền tù tì, vì nhiều lúc chỉ muốn ghé xem nhanh vài thông tin cơ bản. Phần hướng dẫn đăng ký cũng có mục riêng nên không phải mò trong đoạn giới thiệu để tìm. Mấy thông tin về độ uy tín thì mình vẫn muốn đối chiếu thêm ở nguồn khác, chứ trang tự nói gì cũng cần xem lại một chút. Nhưng riêng chuyện sắp xếp nội dung thì thấy khá tiện, nhất là với người mới chưa biết bắt…

Like

giecphangqua.n.h.g.h.u.n.g
Nov 11

88ibet có mấy tiêu đề viết thẳng thành câu hỏi, kiểu “là gì” hay “có uy tín không”, đọc lướt cũng biết từng đoạn đang nói chuyện gì. Mình thích cách chia này hơn là mở ra gặp ngay một khối chữ dài, chưa đọc đã thấy hơi ngán. Nếu chỉ ghé xem thông tin cơ bản thì cứ tìm câu hỏi mình đang thắc mắc rồi đọc phần đó trước, không nhất thiết phải đi từ đầu trang xuống cuối. Còn những điều website tự nói về độ uy tín thì mình vẫn muốn đối chiếu ở nguồn khác, chứ có mục giải thích riêng cũng chưa đủ để kết luận. Nhưng xét riêng phần trình bày nội dung…

Like

normami.l.e.s.940.4
Oct 12

https://soicaulodechuan.net/ đang thấy mọi người bàn về cách trình bày trang kiểu “dễ nhìn hay không” nên mình mở thử luôn cho biết. Mình chỉ lướt vài phút, chủ yếu xem cảm giác dùng thực tế thế nào chứ không đi sâu nội dung, và thấy họ chia các khối thông tin khá rành mạch nên kéo xuống vẫn không bị mất phương hướng. Thanh menu đặt ngay trên đầu, bấm qua lại mấy mục thì phản hồi nhanh, không phải đợi lâu (kiểu nhẹ tay ấy). Mà màu nền với chữ nhìn vừa mắt, không bị chói nên đọc nhanh cũng đỡ mỏi. Nói chung nếu chỉ cần xem nhanh rồi chuyển phần khác thì ổn, vì các bảng/ô…

Like

ngoongniepgangvoat.1.24.5
Sep 06

https://gamebaidoithuong.fyi/ mình vừa tò mò bấm vào thử vì thấy link được nhắc đâu đó, tưởng sẽ vào xem được chút giao diện. Ai dè vừa mở ra là gặp ngay trang báo lỗi “404 Not Found”, kiểu không tìm thấy tài nguyên trên server nên chẳng có gì để lướt tiếp. Cơ mà trang lỗi nhìn cũng khá sạch sẽ, chữ dễ đọc, không bị nhồi nhét linh tinh nên liếc cái là hiểu ngay chuyện gì đang xảy ra. Kéo xuống dưới còn thấy dòng “Proudly powered by LiteSpeed Web Server” nằm ở cuối trang.

Like

toachdiechngiechl.uan
Sep 05

https://uu88sh.com/ mình thấy dùng na ná vài trang mình hay lướt trước đây, kiểu vào phát là nhận ra ngay luồng chính nằm đâu nên khỏi mất công mò, mình chỉ bật lên xem thử chút thôi mà vẫn kéo qua lại khá trơn tru, mấy khối nội dung xếp gọn nên mắt đỡ phải chạy vòng vòng, cảm giác như họ ưu tiên cho người lười đọc hướng dẫn kiểu mình. Mình thao tác vài cái là quen. Không cần bấm nhiều. Có đoạn PHÁ GẠCH mình thấy ghi hướng dẫn rõ ràng ngay cạnh nội dung. Nhất là dòng nói dùng ← → hoặc A D để di chuyển thanh nhìn cái hiểu liền. Phần PHÁ GẠCH để…

Like
bottom of page