How to sort an ArrayList in Java
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Sorting an ArrayList in Java is usually straightforward because the Collections API already provides the needed operations. The real choice is whether your elements have a natural order or whether you need to supply a custom Comparator.
Modern Java code most often uses List.sort, but Collections.sort is still common and works the same way conceptually. Both mutate the list in place.
Sorting Values with Natural Ordering
If the elements already know how to compare themselves, sorting is simple. Types such as Integer, String, and LocalDate already implement Comparable.
Passing null to sort means "use natural ordering." The older equivalent is Collections.sort(numbers).
Duplicates are not a problem. They remain in the list, and Java's sort is stable, so elements that compare equal keep their relative order.
Sorting Objects with a Comparator
When you store custom objects, Java needs to know what "sorted" means. You can encode that rule with a Comparator.
This is often better than making the class implement Comparable when the ordering rule is specific to one use case.
Collections.sort Versus List.sort
Both approaches sort the same way. The practical difference is style:
- '
Collections.sort(list)is older and still perfectly valid' - '
list.sort(comparator)is shorter and fits naturally with lambda expressions'
For new code, many developers prefer list.sort(...) because it keeps the action on the list itself. But there is no major algorithmic difference between the two.
Sorting Without Changing the Original List
Because sorting mutates the list, copy the data first if the original order matters:
This small step is useful in tests, UI code, and anywhere the same collection must be viewed in more than one order.
Descending Order and Null Handling
Descending order is easy with comparator helpers:
If your list may contain null, handle that explicitly:
Without a null-aware comparator, sorting a list that contains null may throw a NullPointerException.
Case-insensitive sorting is another common variant:
That pattern is useful when display order should feel natural to users rather than following raw Unicode code-point order.
Common Pitfalls
- Forgetting that sorting changes the original
ArrayListin place. - Trying to sort custom objects without either
Comparableor aComparator. - Assuming duplicates are removed. Sorting changes order, not membership.
- Ignoring
nullvalues when the list can contain them.
Summary
- Use
list.sort(null)orCollections.sort(list)for values with natural ordering. - Use a
Comparatorfor custom sort logic. - '
List.sortandCollections.sortare similar in behavior;List.sortis often cleaner in modern Java.' - Sorting is in place, so it mutates the original list.
- Handle duplicates and
nullvalues intentionally when designing your comparator.
Related reading
- How to sort an IEnumerablestring
- How to sort an object array by date property?
- How to sort array suffixes in block sorting
- How to sort by two fields in Java?
- 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 Counter by value? - python
- How to sort depended objects by dependency

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.