DEV Community

Dev Cookies
Dev Cookies

Posted on

🧠 Mastering Sorting of Pairs and Custom Objects in Java: A Complete Guide

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]));
Enter fullscreen mode Exit fullscreen mode

▢️ Sort by second element (descending):

Arrays.sort(arr, (a, b) -> Integer.compare(b[1], a[1]));
Enter fullscreen mode Exit fullscreen mode

❌ 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]));
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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; }
}
Enter fullscreen mode Exit fullscreen mode

▢️ 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));
Enter fullscreen mode Exit fullscreen mode

▢️ Descending sort:

list.sort(Comparator.comparingInt((Pair p) -> p.second).reversed());
Enter fullscreen mode Exit fullscreen mode

βœ… 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]));
Enter fullscreen mode Exit fullscreen mode

▢️ With objects:

PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.second));
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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));
Enter fullscreen mode Exit fullscreen mode

▢️ Descending:

entries.sort(Comparator.comparingInt(Map.Entry::getValue).reversed());
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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());
Enter fullscreen mode Exit fullscreen mode

▢️ TreeSet with custom objects:

TreeSet<Pair> set = new TreeSet<>(Comparator.comparingInt(p -> p.second));
Enter fullscreen mode Exit fullscreen mode

⚠️ 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

  1. Use Comparator.comparingInt() for clean, readable sorting when dealing with objects.
  2. Use lambdas when working with primitive arrays or quick-and-dirty code.
  3. Always use .reversed() when sorting in descending order.
  4. For multiple sort keys (e.g., by value then key), use thenComparing():
   list.sort(Comparator.comparingInt((Pair p) -> p.second)
                       .thenComparingInt(p -> p.first));
Enter fullscreen mode Exit fullscreen mode

🧾 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)