W3docs

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 case

Comparator 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.

ApproachUse 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 (AZ) sorts before every lowercase letter (az). "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.

java— editable, runs on the server

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_ORDER ignores case and yields [alice, Bob, Charlie].
  • The length comparator gives [Bob, alice, Charlie] — 3, then 5, then 7 letters.
  • The original list still prints [Bob, Charlie, alice], proving each sort ran on a copy, not the source.

Practice

Practice

Which call sorts an ArrayList of Strings into descending order?