Java Arrays.sort() Method

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

Definition

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.

Java
int[] n = {3, 1, 2};
Arrays.sort(n);                               // [1, 2, 3]
String[] s = {"pear", "Fig", "apple"};
Arrays.sort(s);                               // [Fig, apple, pear]
Arrays.sort(s, String.CASE_INSENSITIVE_ORDER); // [apple, Fig, pear]
CallWhat it does
Arrays.sort(a)Ascending order, whole array.
Arrays.sort(a, from, to)Sorts only indexes from to to - 1.
Arrays.sort(objs, comparator)Custom order for object arrays.
Arrays.sort(boxed, Collections.reverseOrder())Descending order; needs Integer[], not int[].

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

ParameterDescription
aThe 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.

Return Value

Return ValueDescription
voidNothing. The array is sorted in place.

Compare with Similar Methods

Related ways to order data:

MethodHow it differs
Arrays.parallelSort(a)Same result; may use several threads for large arrays.
Arrays.binarySearch(a, key)Fast search that requires a sorted array.
list.sort(c)Sorts an ArrayList instead of an array.
Arrays.compare(x, y)Orders whole arrays against each other (Java 9).

Java Examples

Example 1 — Sort Numbers and Strings

Sort ages, then sort words with and without ignoring case.

Java
import java.util.Arrays;

public class ArraysSortDemo {
    public static void main(String[] args) {
        int[] ages = {34, 12, 56, 7, 23};
        Arrays.sort(ages);
        System.out.println(Arrays.toString(ages));

        String[] words = {"banana", "Cherry", "apple"};
        Arrays.sort(words);
        System.out.println(Arrays.toString(words));

        Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);
        System.out.println(Arrays.toString(words));
    }
}

How It Works

  1. Numeric order. The int[] overload puts 7 first and 56 last.
  2. Uppercase first. Natural String order compares character codes, and 'C' (67) is smaller than 'a' (97), so Cherry leads.
  3. Ignoring case. String.CASE_INSENSITIVE_ORDER compares the words as if they had the same case, which gives dictionary order.
  4. Sorted again in place. The second sort rearranges the same words array; no new array is created.

Example 2 — Two Ways to Sort in Descending Order

Use a reverse comparator on an Integer[], or sort an int[] and reverse it.

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

public class ArraysSortDescending {
    public static void main(String[] args) {
        Integer[] boxed = {5, 1, 4, 2, 3};
        Arrays.sort(boxed, Collections.reverseOrder());
        System.out.println("Integer[] desc: " + Arrays.toString(boxed));

        int[] raw = {5, 1, 4, 2, 3};
        Arrays.sort(raw);
        for (int i = 0, j = raw.length - 1; i < j; i++, j--) {
            int tmp = raw[i];
            raw[i] = raw[j];
            raw[j] = tmp;
        }
        System.out.println("int[] desc:     " + Arrays.toString(raw));
    }
}

How It Works

  1. Comparators need objects. Collections.reverseOrder() is a Comparator, which only object arrays such as Integer[] accept.
  2. No int[] comparator. Arrays.sort(raw, Collections.reverseOrder()) would not compile: there is no sort(int[], Comparator).
  3. Sort, then flip. The int[] is sorted ascending; then two indexes walk toward the middle, swapping elements.
  4. 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");
        }
    }
}

How It Works

  1. Sort key. Comparator.comparingInt(s -> s.grade) orders students by the grade field only.
  2. 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.
  3. No natural order. Student does not implement Comparable, so sorting without a comparator fails with ClassCastException when two elements are compared.
  4. 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.

Next: Arrays.spliterator()

Get a Spliterator over an array or a range of it.

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

15 people found this page helpful