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.
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
Parameter
Description
list
The list to sort. It must allow set, so read-only lists fail.
c
The Comparator that decides the order. null means natural order.
Return Value
Return Value
Description
void
Nothing. The list you passed in is now sorted.
Compare with Similar Methods
Other ways to put elements in order:
Method
How it differs
list.sort(cmp)
Same result; called on the list instead of the helper class.
Two copies. Each sort works on its own ArrayList copy, so both start from the order banana, apple, Cherry, Apple.
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.
Ignoring case.String.CASE_INSENSITIVE_ORDER treats apple and Apple as equal, giving the order a reader expects.
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);
}
}
No natural order.Task does not implement Comparable, so the one-argument sort would not even compile here.
A key extractor.Comparator.comparingInt(t -> t.priority) builds a comparator that orders tasks by their int priority.
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.
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.