How to sort by two fields in Java?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Sorting collections of objects by multiple fields is a common necessity in Java programming, especially when dealing with complex data models. Here's a detailed guide on how to sort by two fields in Java.
Introduction
When working with lists of objects, it's often necessary to impose an order. For instance, consider a list of Person objects, where you want to sort first by the last name and then by the first name. Java provides several ways to achieve this, which we will explore in this article using the Comparator interface and Java 8+ features like lambda expressions and method references.
Comparator in Java
The Comparator interface in Java is a powerful feature that allows objects to be ordered by defining custom sorting logic.
Using Comparator Prior to Java 8
Before Java 8, sorting a list by multiple fields involved implementing a Comparator class or using anonymous classes. Here’s how you can do it:
- Define a class implementing
Comparator:
Using Comparator in Java 8 and Later
Java 8 introduced lambda expressions and the Comparator interface’s static and default methods, which made sorting more concise and readable.
- Using Lambda Expressions:
- Using Method References:
Java 8 also supports method references, which can make your code cleaner:
Details on Comparator Methods
Comparator.comparing(): This method generates aComparatorfor a specific field using a key extractor function.thenComparing(): Allows additional specifications for theComparator, enabling multi-level sorting.
Performance Considerations
Sorting algorithms in Java utilize mergesort or timsort (a derivative of mergesort). Both are stable and run in O(n log n) time complexity. Using multi-level sorting doesn’t change this complexity since each additional level of sorting is linear O(n) relative to the already sorted structure.
Summary Table
| Feature | Description |
Comparator.comparing() | Generates a Comparator for a specific field. |
thenComparing() | Chaining method for multi-level sorting. |
| Lambda Expressions | Provides in-line, anonymous implementation of Comparator. |
| Method References | Cleaner syntax for referencing methods directly. |
| Time Complexity | O(n log n) due to timsort/mergesort. |
| Stability | Preserves order of equal elements across different fields. |
Conclusion
Sorting by multiple fields in Java is a straightforward task, especially with the enhancements provided in Java 8 and beyond. The use of Comparator, lambda expressions, and method references offers flexibility and cleaner code. Whether dealing with existing collections or custom objects, Java provides robust mechanisms to implement complex sorting.
Related reading
- How to sort Counter by value? - python
- How to sort depended objects by dependency
- How to sort faster than n log n given a strong condition on the list?
- How to sort in-place using the merge sort algorithm?
- How to sort ListFile to list directories first and grouping files by directory?
- How to specify api docs url for swagger ui in spring boot open api v3?
- How to sort List of objects by some property
- How to sort mongodb with pymongo

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.