Whether you're using arrays, lists, priority queues, or maps β sorting pairs (or objects with multiple fields) is a common task in Java. This guide will walk you through the best ways to do it, using:
- β Arrays
- β
Lists (
ArrayList) - β PriorityQueue
- β TreeMap / TreeSet
- β Custom Objects
- β Java 8+ functional comparators
πΉ 1. Arrays of Primitive Arrays (int[][])
β Use Case:
Fastest to use in algorithm problems (e.g. LeetCode), with low overhead.
βΆοΈ Sort by second element (ascending):
int[][] arr = { {1, 5}, {2, 3}, {3, 8}, {4, 1} };
Arrays.sort(arr, (a, b) -> Integer.compare(a[1], b[1]));
βΆοΈ Sort by second element (descending):
Arrays.sort(arr, (a, b) -> Integer.compare(b[1], a[1]));
β You cannot use Comparator.comparingInt() here because int[] is not an object with named fields.
πΉ 2. List of Arrays (List<int[]>)
Same as arrays but with Collections.sort.
βΆοΈ Sort by second element ascending:
List<int[]> list = new ArrayList<>();
list.add(new int[]{1, 5});
list.add(new int[]{2, 3});
Collections.sort(list, (a, b) -> Integer.compare(a[1], b[1]));
πΉ 3. List of Custom Objects
Define a Pair or any domain object with named fields.
βΆοΈ Pair class:
class Pair {
int first, second;
Pair(int f, int s) { first = f; second = s; }
}
βΆοΈ Ascending sort by second:
List<Pair> list = new ArrayList<>();
list.add(new Pair(1, 5));
list.add(new Pair(2, 3));
list.sort(Comparator.comparingInt(p -> p.second));
βΆοΈ Descending sort:
list.sort(Comparator.comparingInt((Pair p) -> p.second).reversed());
β Clean and readable β use this for production-quality code.
πΉ 4. PriorityQueue of Pairs
βΆοΈ With arrays:
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(a[1], b[1]));
βΆοΈ With objects:
PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.second));
πΉ 5. Map.Entry<K, V> (like HashMap or TreeMap)
β Use Case:
When you want to sort a map by value or keys, use Map.Entry.
βΆοΈ Sort map by value ascending:
Map<String, Integer> map = new HashMap<>();
map.put("a", 3); map.put("b", 1);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
entries.sort(Comparator.comparingInt(Map.Entry::getValue));
βΆοΈ Descending:
entries.sort(Comparator.comparingInt(Map.Entry::getValue).reversed());
πΉ 6. TreeMap and TreeSet Custom Sorting
β Use Case:
To maintain a continuously sorted collection.
βΆοΈ TreeMap sorted by custom comparator on keys:
TreeMap<Integer, String> tree = new TreeMap<>(Comparator.reverseOrder());
βΆοΈ TreeSet with custom objects:
TreeSet<Pair> set = new TreeSet<>(Comparator.comparingInt(p -> p.second));
β οΈ Note: TreeSet requires consistent equals() and compareTo() logic to avoid silent rejection of duplicates.
β Summary of Comparator Options
| Collection Type | Use Lambda |
comparingInt Works |
Needs Custom Class | Reverse Order |
|---|---|---|---|---|
int[][] / List<int[]>
|
β Yes | β No | β No | β Yes (manually) |
List<Pair> |
β Yes | β Yes | β Yes | β Yes |
PriorityQueue<Pair> |
β Yes | β Yes | β Yes | β Yes |
Map.Entry<K, V> |
β Yes | β Yes | β No | β Yes |
TreeSet, TreeMap
|
β Yes | β Yes | β Recommended | β Yes |
π Pro Tips
-
Use
Comparator.comparingInt()for clean, readable sorting when dealing with objects. - Use lambdas when working with primitive arrays or quick-and-dirty code.
- Always use
.reversed()when sorting in descending order. - For multiple sort keys (e.g., by value then key), use
thenComparing():
list.sort(Comparator.comparingInt((Pair p) -> p.second)
.thenComparingInt(p -> p.first));
π§Ύ Closing Thoughts
Sorting in Java is a versatile tool β once you understand how to use Comparator, comparingInt, and method references effectively, your code becomes cleaner, safer, and easier to maintain.
Choose the right strategy based on:
- Your data structure (array, list, map)
- Whether you need ascending/descending
- Whether your data has named fields
\
Top comments (0)