In Java, Arrays.sort(a) rearranges the elements of a into ascending order. It changes the array you pass in and returns nothing. Numbers sort from small to large; strings sort by their character codes, so uppercase letters come before lowercase ones.
Object arrays can be sorted by their natural order (the class must implement Comparable) or by a Comparator you pass in. A range version sorts only the indexes from fromIndex up to, but not including, toIndex.
A static method of java.util.Arrays (Java 1.2+): call it on the class, as in Arrays.sort(...); there is no Arrays object to create.
Syntax
Sort the whole array, a range, or with a Comparator.
Primitive overloads exist for int, long, short, char, byte, float, and double (not boolean), each with a range version. Comparator overloads exist only for object arrays.
Library declarations
Java
// java.util.Arrays (since Java 1.2)
public static void sort(int[] a)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(Object[] a)
public static <T> void sort(T[] a, Comparator<? super T> c)
// Object[] and T[] + Comparator also have range forms
// same pair for long[], short[], char[], byte[], float[], double[]
Parameters
Parameter
Description
a
The array to sort. Must not be null.
fromIndex, toIndex (optional)
Range to sort: fromIndex inclusive, toIndex exclusive.
c (optional)
A Comparator for object arrays; null means natural order.
Comparators need objects.Collections.reverseOrder() is a Comparator, which only object arrays such as Integer[] accept.
No int[] comparator.Arrays.sort(raw, Collections.reverseOrder()) would not compile: there is no sort(int[], Comparator).
Sort, then flip. The int[] is sorted ascending; then two indexes walk toward the middle, swapping elements.
Same answer, no boxing. Both end as 5, 4, 3, 2, 1; the second way creates no Integer objects.
Example 3 — Sort Objects by a Field, and a Class That Is Not Comparable
Sort students by grade with a comparator, then try to sort without one.
Java
import java.util.Arrays;
import java.util.Comparator;
public class ArraysSortObjects {
static class Student {
final String name;
final int grade;
Student(String name, int grade) { this.name = name; this.grade = grade; }
@Override
public String toString() { return name + "(" + grade + ")"; }
}
public static void main(String[] args) {
Student[] students = {
new Student("Mo", 2), new Student("Al", 1), new Student("Jo", 2), new Student("Bea", 1)
};
Arrays.sort(students, Comparator.comparingInt(s -> s.grade));
System.out.println(Arrays.toString(students));
try {
Arrays.sort(new Object[] {new Student("X", 1), new Student("Y", 2)});
} catch (ClassCastException e) {
System.out.println("Student is not Comparable: ClassCastException");
}
}
}
Output
[Al(1), Bea(1), Mo(2), Jo(2)]
Student is not Comparable: ClassCastException
How It Works
Sort key.Comparator.comparingInt(s -> s.grade) orders students by the grade field only.
Stable ties. Al and Bea share grade 1, and Mo and Jo share grade 2; each pair keeps its original relative order because object sorting is stable.
No natural order.Student does not implement Comparable, so sorting without a comparator fails with ClassCastException when two elements are compared.
Readable output. The overridden toString() gives Al(1) instead of a class name and hash.
Frequently Asked Questions
No. It returns void and sorts the array you pass in. Copy it first with clone() if you need the original order.
There is no Comparator overload for int[]. Sort ascending and reverse the array with a loop, or use an Integer[] with Collections.reverseOrder().
For object arrays, yes: equal elements keep their relative order. For primitive arrays stability does not matter, because equal values are indistinguishable.
The elements do not implement Comparable, or they cannot be compared with each other. Pass a Comparator, or make the class implement Comparable.
Use Arrays.sort(a, fromIndex, toIndex). Elements outside that range are not moved.