This article will discuss various methods to sort an array of primitives types or objects in Java.

1. Using Arrays.sort() method

The Arrays class provides several static methods for sorting arrays. The Arrays.sort(Object[]) method sorts the specified array of primitives types or objects into ascending order, according to the natural ordering of its elements. Arrays.sort() uses Dual-Pivot Quicksort that offers O(n.log(n)) performance, which is typically faster than traditional (one-pivot) Quicksort implementations. It also has a version that sorts the specified array between the specified range.

 
The Arrays.sort(T[], Comparator) method sorts the specified array of objects according to the order induced by the specified comparator. It requires all array elements to be mutually comparable by the specified comparator, i.e., for any pair of elements (e1, e2) in the array, c.compare(e1, e2) should not throw a ClassCastException.

 
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., it will maintain the relative order of equal elements. It uses iterative Merge Sort that requires far fewer than n.log(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. Using Arrays.parallelSort() method

Java 8 also provides Arrays.parallelSort() which uses multiple threads for sorting as opposed to Arrays.sort() which uses only a single thread to sort the elements. The prototype of the parallelSort() is similar to sort(). It beats sort() when the total number of elements crosses a certain threshold. Internally, any array of size lesser than the threshold value is sorted using the sort(). The threshold is calculated by considering the parallelism of the machine and the size of the array. The ForkJoin common pool is used to execute any parallel tasks. Following Java program compares the performance of parallelSort() with sort() on a huge data set of 10 million integers:

Download Code

Possible Output:

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

3. Using Stream API

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

4. Using Collections.sort() method

We know that the 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 use this fact to sort a list returned by Arrays.asList() using the Collections.sort() method, which in turn sort the original array as well. This method will not work on an array of primitives as it requires the inferred type to implement Comparable.

That’s all about sorting an array in Java.

 
Reference: Arrays (Java Platform SE 21)