How to Sort an ArrayList in Java
Sort a Java ArrayList using Collections.sort, List.sort, Comparable, and Comparator.
How to Sort an ArrayList in Java
Sorting an ArrayList reorders its elements in place. The JDK gives you two entry points — Collections.sort and the List.sort instance method — plus Comparator for any custom or reverse ordering. This chapter shows the idiomatic ways to do it.
Sort in Natural Order
For elements that implement Comparable (such as String, Integer, or LocalDate), Collections.sort sorts them in their natural order.
List<String> names = new ArrayList<>(List.of("Charlie", "Bob", "Alice"));
Collections.sort(names);
System.out.println(names); // [Alice, Bob, Charlie]The same result comes from the instance method names.sort(null) — passing null as the comparator means "use natural order." Sorting happens in place, so the original list is reordered rather than copied.
Sort with List.sort and a Comparator
Since Java 8, List has its own sort(Comparator) method. It reads more fluently and pairs naturally with the Comparator factory methods.
List<String> names = new ArrayList<>(List.of("Charlie", "Bob", "Alice"));
names.sort(Comparator.naturalOrder()); // ascending
names.sort(Comparator.reverseOrder()); // descending
names.sort(String.CASE_INSENSITIVE_ORDER); // ignore letter caseComparator also composes. Comparator.comparingInt(String::length).thenComparing(Comparator.naturalOrder()) sorts by length first and breaks ties alphabetically. For objects, Comparator.comparing(Person::lastName) keys off any field.
| Approach | Use when |
|---|---|
Collections.sort(list) | Natural order, older codebases |
list.sort(Comparator) | Modern, readable, custom orders |
Comparator.reverseOrder() | Descending natural order |
Comparator.comparing(...) | Sort objects by a field |
Watch Out for Default String Order
Natural String order is based on Unicode code points, so every uppercase letter (A–Z) sorts before every lowercase letter (a–z). "Bob" lands before "alice".
List<String> mixed = new ArrayList<>(List.of("alice", "Bob"));
mixed.sort(Comparator.naturalOrder());
System.out.println(mixed); // [Bob, alice]If you want case-insensitive ordering, use String.CASE_INSENSITIVE_ORDER. For language-aware ordering (accents, locale rules), use a java.text.Collator instead.
A Runnable Example
This example starts from one list, then sorts copies of it four different ways so you can compare the results side by side. The original list is left untouched.
What to take from the run:
- Natural order prints
[Bob, Charlie, alice]because uppercase code points sort before lowercase ones. Comparator.reverseOrder()flips that natural order to[alice, Charlie, Bob].String.CASE_INSENSITIVE_ORDERignores case and yields[alice, Bob, Charlie].- The length comparator gives
[Bob, alice, Charlie]— 3, then 5, then 7 letters. - The
originallist still prints[Bob, Charlie, alice], proving each sort ran on a copy, not the source.
Practice
Which call sorts an ArrayList of Strings into descending order?