Java ArrayList sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java 8 and later, sort(c) reorders the elements of an ArrayList in place according to the Comparator c. Passing null sorts by the elements' natural order (numbers ascending, strings alphabetically by character code), which requires the elements to be Comparable.

The sort is stable: elements that compare as equal keep their original relative order. That is what makes sorting first by one key and then by another work as expected. Collections.sort(list) simply calls list.sort(null) since Java 8.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe. sort(Comparator) was added to List in Java 8. Before that, use Collections.sort(list) or Collections.sort(list, comparator).

Syntax

Pass a comparator, or null for natural order.

Java
List<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 2));
nums.sort(null);                           // [1, 2, 3]
nums.sort(Comparator.reverseOrder());      // [3, 2, 1]
names.sort(Comparator.comparing(String::length));
CallWhat it does
list.sort(null)Natural order (ascending).
list.sort(Comparator.reverseOrder())Natural order, reversed.
list.sort(Comparator.comparing(key))Sorts by a key such as length or name.
list.sort(String.CASE_INSENSITIVE_ORDER)Strings alphabetically, ignoring case.

sort changes the list itself and returns nothing. To keep the original order too, sort a copy: List<T> sorted = new ArrayList<>(list); sorted.sort(c);

Library declarations
Java
// java.util.ArrayList<E>
public void sort(Comparator<? super E> c)

// Declared in List<E> (default method since Java 8)

Parameters

ParameterDescription
c (Comparator<? super E>)Decides the order of two elements. null means natural order.

Return Value

Return ValueDescription
voidNothing; the list itself is reordered.

Compare with Similar Methods

Sorting and reordering:

MethodHow it differs
Collections.sort(list)The older form; calls list.sort(null).
replaceAll(op)Changes the values but keeps their positions.
subList(a, b).sort(c)Sorts only part of the list.
getFirst()After sorting, reads the smallest element (Java 21+).

Java Examples

Example 1 — Sort Numbers and Strings

Sort numbers both ways, then see how capital letters affect string order.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ArrayListSortDemo {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(Arrays.asList(42, 7, 19, 3));
        nums.sort(null);
        System.out.println("ascending:  " + nums);
        nums.sort(Comparator.reverseOrder());
        System.out.println("descending: " + nums);

        List<String> words = new ArrayList<>(Arrays.asList("banana", "Cherry", "apple"));
        words.sort(null);
        System.out.println("natural:    " + words);
        words.sort(String.CASE_INSENSITIVE_ORDER);
        System.out.println("ignoreCase: " + words);
    }
}

How It Works

  1. Natural order. sort(null) uses Integer.compareTo, so the numbers go from smallest to largest.
  2. Reverse. Comparator.reverseOrder() flips natural order, giving largest first.
  3. Uppercase first. Strings compare by character code, and every capital letter comes before every small letter, so "Cherry" sorts ahead of "apple".
  4. Ignore case. String.CASE_INSENSITIVE_ORDER compares the words as if they were all the same case, giving true alphabetical order.

Example 2 — Sort by Length, Then Alphabetically

Sort city names by length, then add a second key to break ties.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ArrayListSortComparator {
    public static void main(String[] args) {
        List<String> cities = new ArrayList<>(Arrays.asList("Rome", "Oslo", "Lima", "Paris", "Bern", "Cairo"));

        Comparator<String> byLength = Comparator.comparing(String::length);
        cities.sort(byLength);
        System.out.println("by length:        " + cities);

        cities.sort(byLength.thenComparing(Comparator.naturalOrder()));
        System.out.println("length, then a-z: " + cities);
    }
}

How It Works

  1. Key extractor. Comparator.comparing(String::length) compares cities by their number of letters only.
  2. Stable ties. Rome, Oslo, Lima, and Bern all have 4 letters, so they keep their original order; Paris and Cairo (5 letters) do the same.
  3. Tie-breaker. thenComparing(Comparator.naturalOrder()) is used only when two lengths are equal, and it orders those names alphabetically.
  4. Final order. The 4-letter group becomes Bern, Lima, Oslo, Rome, followed by Cairo and Paris.

Example 3 — Sorting a List That Contains null

Natural order fails on null; nullsLast and nullsFirst fix it.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ArrayListSortNulls {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(Arrays.asList("Zed", null, "Amy", "Kai"));

        try {
            names.sort(null);
        } catch (NullPointerException e) {
            System.out.println("natural order: NullPointerException");
        }

        names.sort(Comparator.nullsLast(Comparator.naturalOrder()));
        System.out.println("nullsLast:  " + names);

        names.sort(Comparator.nullsFirst(Comparator.reverseOrder()));
        System.out.println("nullsFirst: " + names);
    }
}

How It Works

  1. null cannot compare. Natural order calls compareTo on the elements, and comparing with null throws a NullPointerException.
  2. Wrap the comparator. Comparator.nullsLast(...) treats null as larger than everything and uses the inner comparator for the rest.
  3. Alphabetical, null at the end. Amy, Kai, Zed are in natural order and the null comes last.
  4. Combine freely. nullsFirst(Comparator.reverseOrder()) puts null first and the names from Z to A.

Frequently Asked Questions

Call list.sort(null) for natural order, or pass a Comparator such as Comparator.reverseOrder() or Comparator.comparing(...).
Since Java 8, Collections.sort(list) just calls list.sort(null), so the result is the same. list.sort is the newer, shorter form.
Use list.sort(Comparator.reverseOrder()), or comparator.reversed() for a custom comparator.
Yes. Elements that compare as equal keep their original relative order.
The list contains a null element and natural order cannot compare it. Use Comparator.nullsFirst or nullsLast.

Next: spliterator()

Split the list for streams and parallel work (Java 8+).

spliterator() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful