Java Collections sort() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.sort(list) rearranges the elements of a List into ascending natural order: numbers from small to large, strings in dictionary (Unicode) order. The list itself is changed; nothing is returned.

The second overload, Collections.sort(list, comparator), lets you decide the order yourself. Both versions are stable: elements that compare as equal keep the order they had before the sort, which matters when you sort records by one field.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.sort(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass a changeable list, and optionally a Comparator.

Java
List<String> names = new ArrayList<>(Arrays.asList("Cy", "Ana", "Ben"));
Collections.sort(names);                             // [Ana, Ben, Cy]
Collections.sort(names, Comparator.reverseOrder());  // [Cy, Ben, Ana]
Collections.sort(names, null);                       // null = natural order
CallWhat it does
Collections.sort(list)Natural order; elements must implement Comparable.
Collections.sort(list, cmp)Order decided by the Comparator.
Collections.sort(list, null)Same as the one-argument version.
list.sort(cmp)The List method (Java 8+) that Collections.sort calls for you.

Since Java 8, Collections.sort(list, c) simply calls list.sort(c). The algorithm is a merge sort variant (TimSort): about n log n comparisons, and very fast on partly sorted lists.

Library declarations
Java
// java.util.Collections
public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)

Parameters

ParameterDescription
listThe list to sort. It must allow set, so read-only lists fail.
cThe Comparator that decides the order. null means natural order.

Return Value

Return ValueDescription
voidNothing. The list you passed in is now sorted.

Compare with Similar Methods

Other ways to put elements in order:

MethodHow it differs
list.sort(cmp)Same result; called on the list instead of the helper class.
Arrays.sort(arr)Sorts an array instead of a List.
reverseOrder()Gives a comparator for descending order to pass to sort.
reverse(list)Only flips the current order; it does not compare anything.

Java Examples

Example 1 — Natural Order Puts Capitals First

Sort the same four words twice: once in natural order and once ignoring case.

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

public class CollectionsSortNatural {
    public static void main(String[] args) {
        List<String> words = Arrays.asList("banana", "apple", "Cherry", "Apple");

        List<String> natural = new ArrayList<>(words);
        Collections.sort(natural);
        System.out.println("Natural:          " + natural);

        List<String> ignoreCase = new ArrayList<>(words);
        Collections.sort(ignoreCase, String.CASE_INSENSITIVE_ORDER);
        System.out.println("Case-insensitive: " + ignoreCase);
    }
}

How It Works

  1. Two copies. Each sort works on its own ArrayList copy, so both start from the order banana, apple, Cherry, Apple.
  2. Character codes decide. In natural order, every uppercase letter (A = 65, C = 67) comes before every lowercase letter (a = 97), so Cherry lands before apple.
  3. Ignoring case. String.CASE_INSENSITIVE_ORDER treats apple and Apple as equal, giving the order a reader expects.
  4. Stability shows. Because apple and Apple tie, they stay in their original relative order: lowercase apple was first in the input, so it stays first.

Example 2 — Sort Tasks by Priority with a Comparator

Objects without a natural order can still be sorted by a field. Equal priorities keep their insertion order.

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

public class CollectionsSortTasks {
    static class Task {
        final String name;
        final int priority;

        Task(String name, int priority) {
            this.name = name;
            this.priority = priority;
        }

        @Override
        public String toString() {
            return name + "(" + priority + ")";
        }
    }

    public static void main(String[] args) {
        List<Task> tasks = new ArrayList<>();
        tasks.add(new Task("email", 2));
        tasks.add(new Task("deploy", 1));
        tasks.add(new Task("lunch", 3));
        tasks.add(new Task("review", 1));
        tasks.add(new Task("backup", 2));

        Collections.sort(tasks, Comparator.comparingInt(t -> t.priority));
        System.out.println(tasks);
    }
}

How It Works

  1. No natural order. Task does not implement Comparable, so the one-argument sort would not even compile here.
  2. A key extractor. Comparator.comparingInt(t -> t.priority) builds a comparator that orders tasks by their int priority.
  3. Ties stay put. deploy and review both have priority 1; deploy was added first, so it is still first. The same happens for email and backup at priority 2.
  4. Readable output. The overridden toString prints each task as name(priority) inside the list brackets.

Example 3 — Array Views, Read-Only Lists, and null

Sorting an Arrays.asList view sorts the array itself. A read-only list and a list holding null both fail.

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

public class CollectionsSortErrors {
    public static void main(String[] args) {
        String[] arr = {"pear", "fig", "kiwi"};
        Collections.sort(Arrays.asList(arr));
        System.out.println("Array after sorting its view: " + Arrays.toString(arr));

        List<String> locked = Collections.unmodifiableList(new ArrayList<>(Arrays.asList("b", "a")));
        try {
            Collections.sort(locked);
        } catch (UnsupportedOperationException e) {
            System.out.println("Read-only list: " + e.getClass().getSimpleName());
        }

        List<String> withNull = new ArrayList<>(Arrays.asList("b", null, "a"));
        try {
            Collections.sort(withNull);
        } catch (NullPointerException e) {
            System.out.println("List with null: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Write-through view. Arrays.asList(arr) is backed by the array, so sorting the view rearranges arr itself, as Arrays.toString shows.
  2. Read-only wrapper. The list from unmodifiableList refuses to be reordered and throws UnsupportedOperationException.
  3. null has no order. Natural ordering calls compareTo, which cannot work with null, so the third sort throws NullPointerException.
  4. Fixing the null case. If null values are expected, pass Comparator.nullsFirst(Comparator.naturalOrder()) as the second argument.

Frequently Asked Questions

It sorts the elements of a List in place, in ascending natural order or in the order given by a Comparator. It returns nothing.
Yes. Elements that compare as equal keep their original relative order.
There is none in practice. Since Java 8, Collections.sort calls list.sort internally.
Pass a reversed comparator: Collections.sort(list, Collections.reverseOrder()).
The list cannot be changed, for example because it came from Collections.unmodifiableList, List.of, or Collections.emptyList with elements expected.

Next: swap()

Exchange the elements at two positions in a list.

swap() 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.

13 people found this page helpful