Java SE API

List.sort(Comparator)

Sort in place stably; elements must support the requested order and replacement.

Formal Syntax & Structure

java syntaxjava
default void sort(Comparator<? super E> c)

Parameters & Attributes

NameTypeDescription
cComparator<? super E>Comparison rule, or null for natural ordering.

Working Example

Example Snippetjava
import java.util.*;public class Main {
    public static void main(String[] args) {        List<Integer> source = List.of(8, 2, 5, 2);        List<Integer> sorted = new ArrayList<>(source);        sorted.sort(null);        System.out.println(source);        System.out.println(sorted);        int[] a = {9, 1, 4};        Arrays.sort(a);        System.out.println(Arrays.toString(a));    }}
Java Reference