Sort Array in Java

In this article, we will discuss various methods to sort array of primitives types or Objects in Java.


1. Sort array using Arrays.sort()

Arrays class provides several static methods for sorting arrays -.


1.1. sort(Object[] a)

It sorts the specified array of primitives types or objects into ascending order, according to the natural ordering of its elements.

It also has a version that sorts the specified array between the specified range –

Arrays.sort() uses Dual-Pivot Quicksort that offers O(n log(n)) performance. It is typically faster than traditional (one-pivot) Quicksort implementations.


1.2. sort(T[] a, Comparator c)

It sorts the specified array of objects according to the order induced by the specified comparator. It requires all elements of the array to be mutually comparable by the specified comparator. i.e. for any pair of elements (e1, e2) in the array,, e2) should not throw a ClassCastException.

To sort in Ascending order:


To sort in Descending order:

We can also write our custom comparator as shown below:

It also provides a version that sorts the specified array of objects between the specified range according to the order induced by the specified comparator –

This sort results in a stable sort i.e. relative order of equal elements will be maintained. It uses iterative Merge Sort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, else offering the performance of a traditional merge sort when the input array is randomly ordered.


2. Sort array using Arrays.parallelSort()

Java 8 also provides Arrays.parallelSort() which uses multiple threads for sorting as opposed to Arrays.sort() which uses only single thread to sort the elements. The prototype of parallelSort() is simlar to sort().

It beats Arrays.sort() when number of elements cross a certain threshold. Internally, any array of size lesser than the threshold value is sorted using the Arrays.sort(). The threshold is calculated by considering the parallelism of the machine and size of the array. The ForkJoin common pool is used to execute any parallel tasks.

Below java program compares the performance of Arrays.parallelSort() with Arrays.sort() on huge data set of 10 million integers:




Time taken by Arrays.sort() : 1678ms
Time taken by Arrays.parallelSort() : 780ms


3. Sort array using Java 8

We can also use Java 8 Stream API to sort an array. The idea is to get sequential Stream from elements of the specified array and sort it according to natural order or in reverse order using a Comparator. Finally, we convert the sorted stream back to the array.

3.1. To sort primitive array in natural order:


3.2. To sort array of Objects in ascending order:


3.3. To sort of Objects in descending order:


4. Sort array using Collections.sort()

We know that Arrays.asList() method returns a fixed-size list backed by the specified array. That means any changes in the original array will be reflected in the returned list. We can make use of this fact to sort a list returned by Arrays.asList() using Collections.sort() method which in turn sort the original array as well. This method will not work on array of primitives as it requires the inferred type to implement Comparable.

References: Arrays (Java Platform SE 8 )

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)


Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂

Leave a Reply

Notify of