Using Array Sort in Java

To sort an array in Java, use the Arrays.sort() method. It sorts the array in ascending order, from the smallest number to the largest. To sort an Integer or String array in descending order, pass Collections.reverseOrder() as the second argument. For an int array, sort it and then reverse it. To sort objects by your own rule, pass a Comparator instead. In this Java Sort Array Example, we sort an array of five numbers. Click Execute to run the Java Sort Array Example online and see the result.
Using Array Sort in Java Execute
import java.util.Arrays;

public class SortArrayDemo {
    public static void main(String[] args) {
        int[] numbers = {24, 12, 8, 56, 3};
      
      	Arrays.sort(numbers);
      
        System.out.println("Sorted numbers: " + Arrays.toString(numbers));
    }
}

Java Arrays.sort() Syntax

sort() is a static method of the Arrays class. It returns nothing and reorders the array itself. In the second form, fromIndex is inclusive and toIndex is exclusive.

Java Arrays.sort() Syntax
Arrays.sort(array)
Arrays.sort(array, fromIndex, toIndex)
Arrays.sort(array, comparator)

Java Sort Array Examples

The following are examples of sorting an array in Java. You can run any of these examples in the online Java code runner and replace the values to sort your own data.

Sort an int array in ascending order

Arrays.toString() prints the sorted elements. Printing the array itself shows only its type and hash code.

Java Arrays.sort() Example
import java.util.Arrays;

public class SortIntExample {
    public static void main(String[] args) {
        int[] numbers = {42, -7, 15, 0, 8};
        Arrays.sort(numbers);
        System.out.println(Arrays.toString(numbers));
    }
}

// output: [-7, 0, 8, 15, 42]

Sort an array in descending order

Collections.reverseOrder() returns a Comparator that reverses the natural order. It works with Integer[], but not with int[]. To sort an int array in descending order, sort it and then reverse it in a loop.

Java Sort Array Descending Example
import java.util.Arrays;
import java.util.Collections;

public class SortDescendingExample {
    public static void main(String[] args) {
        Integer[] numbers = {42, -7, 15, 0, 8};
        Arrays.sort(numbers, Collections.reverseOrder());
        System.out.println(Arrays.toString(numbers));

        int[] values = {42, -7, 15, 0, 8};
        Arrays.sort(values);
        for (int i = 0; i < values.length / 2; i++) {
            int temp = values[i];
            values[i] = values[values.length - 1 - i];
            values[values.length - 1 - i] = temp;
        }
        System.out.println(Arrays.toString(values));
    }
}

// output:
// [42, 15, 8, 0, -7]
// [42, 15, 8, 0, -7]

Sort an array of strings

Strings are compared by the Unicode values of their characters, so uppercase letters come before lowercase ones. String.CASE_INSENSITIVE_ORDER sorts them ignoring case.

Java Sort String Array Example
import java.util.Arrays;

public class SortStringsExample {
    public static void main(String[] args) {
        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));
    }
}

// output:
// [Cherry, apple, banana]
// [apple, banana, Cherry]

Sort part of an array

Arrays.sort(numbers, 1, 4) sorts the elements at indexes 1, 2 and 3 and leaves the rest in place.

Java Sort Array Range Example
import java.util.Arrays;

public class SortRangeExample {
    public static void main(String[] args) {
        int[] numbers = {9, 7, 5, 3, 1};
        Arrays.sort(numbers, 1, 4);
        System.out.println(Arrays.toString(numbers));
    }
}

// output: [9, 3, 5, 7, 1]

Sort an array of objects by a field

Comparator.comparingInt() builds a Comparator from a field of the object. Here, people are sorted by age.

Java Sort Array of Objects Example
import java.util.Arrays;
import java.util.Comparator;

public class SortObjectsExample {
    static class Person {
        String name;
        int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }

    public static void main(String[] args) {
        Person[] people = {
            new Person("Anna", 31),
            new Person("Ben", 25),
            new Person("Chris", 28)
        };
        Arrays.sort(people, Comparator.comparingInt((Person p) -> p.age));
        for (Person p : people) {
            System.out.println(p.name + " " + p.age);
        }
    }
}

// output:
// Ben 25
// Chris 28
// Anna 31

See also

Updated: