Article:
Sorting lists is a routine but essential task in Java development. Whether you're arranging names alphabetically or sorting objects based on multiple attributes, Java provides several powerful mechanisms to perform sorting. Let’s explore various ways to sort a List
in Java.
✅ 1. Using Collections.sort()
with Comparable
The classic way for sorting a list of elements that implement the Comparable
interface.
java
Collections.sort(list);
✅ 2. Using Collections.sort()
with Comparator
When you want a custom sort without changing the model class:
java
Collections.sort(list, Comparator.comparing(MyClass::getSomeProperty));
✅ 3. Using List.sort()
(Java 8+)
This is a cleaner approach added in Java 8:
java
list.sort(Comparator.comparing(MyClass::getSomeProperty));
✅ 4. Using Java Streams
Create a sorted copy without mutating the original list:
java
List<MyClass> sorted = list.stream()
.sorted(Comparator.comparing(MyClass::getSomeProperty))
.collect(Collectors.toList());
✅ 5. Sorting with Multiple Conditions
Use thenComparing
to handle complex sorting logic:
java
list.sort(
Comparator.comparing(MyClass::getPriority)
.thenComparing(MyClass::getTimestamp)
);
✅ 6. Descending Order
Use reversed()
:
java
list.sort(Comparator.comparing(MyClass::getScore).reversed());
✅ 7. Using TreeSet
for Sorting with Uniqueness
Automatically sorted collection with no duplicates:
java
Set<String> sortedSet = new TreeSet<>(list);
You can also define a custom comparator:
java
Set<Person> sortedByName = new TreeSet<>(Comparator.comparing(Person::getName));
sortedByName.addAll(personList);
✅ 8. Using PriorityQueue
(Heap Sort Behavior)
Useful when you need sorted extraction one by one:
java
PriorityQueue<Integer> pq = new PriorityQueue<>(list);
while (!pq.isEmpty()) {
System.out.println(pq.poll()); // retrieves in sorted order
}
✅ 9. Using Arrays.sort()
with Conversion
For quick sort on a fixed-size list:
java
String[] arr = list.toArray(new String[0]);
Arrays.sort(arr);
List<String> sortedList = Arrays.asList(arr);
✅ 10. Sorting Using Custom Functional Utilities
If you often sort by dynamic fields, utility methods help:
java
public static <T, U extends Comparable<? super U>> void sortByField(List<T> list, Function<T, U> keyExtractor) {
list.sort(Comparator.comparing(keyExtractor));
}
// Usage:
sortByField(personList, Person::getAge);
✅ 11. Sorting with Guava or Apache Commons
If you're using libraries like Guava or Apache Commons Collections, they offer utilities like:
java
Ordering<Person> byName = Ordering.natural().onResultOf(Person::getName);
List<Person> sorted = byName.sortedCopy(personList);
✅ 12. Sorting Immutable Lists (Java 10+)
Create sorted copies using List.copyOf()
or Collectors.toUnmodifiableList()
:
java
List<String> sorted = list.stream()
.sorted()
.collect(Collectors.toUnmodifiableList());
Conclusion
From simple one-liners to advanced functional techniques, Java offers many options for sorting. Choosing the right approach depends on whether you want mutability, performance, readability, or stream-processing style. For most modern applications, the combination of Comparator.comparing()
, lambdas, and streams provides the most expressive and clean syntax.