CKTIS / Java
Arrays.sort — sorting an int[] array
Arrays.sort orders int[] values in ascending order, modifying the supplied array. You can sort the entire array or a selected index range.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
Arrays.sort(a);
Arrays.sort(a, fromIndex, toIndex);Arrays.sort is a static method of java.util.Arrays. The Arrays.sort(int[] a) overload sorts numbers in ascending order within the supplied array. Its return type is void; it does not return a new array. To preserve the original, make a copy first.
The Arrays.sort(a, fromIndex, toIndex) variant includes fromIndex but excludes toIndex. Equal, valid bounds specify an empty range. The API documents IllegalArgumentException for fromIndex > toIndex and ArrayIndexOutOfBoundsException for fromIndex < 0 or toIndex > a.length. It does not specify which range exception takes precedence when more than one of these conditions holds. OpenJDK 21 reads a.length before checking the range, so if a is null, the range overload throws NullPointerException even when fromIndex > toIndex.
The example sorts the entire array; duplicates remain. This description targets Java SE 21; the production runtime has not been attested.
Examples
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] numbers = {4, -2, 4, 1};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}Result
[-2, 1, 4, 4]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.