The sorting is a way to arrange elements of a list or array in a certain order. The order may be in ascending or descending order. The numerical and lexicographical (alphabetical) order is a widely used order.
In this section, we will learn how to sort array in Java in ascending and descending order using the sort() method and without using the sort() method. Along with this, we will also learn how to sort subarray in Java.
Sort Array in Ascending Order
The ascending order arranges the elements in the lowest to highest order. It is also known as natural order or numerical order. We can perform sorting in the following ways:
- Using the sort() Method
- Without using the method
- Using the for Loop
- Using the User Defined Method
Using the sort() Method
In Java, Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting. Its complexity is O(n log(n)). It is a static method that parses an array as a parameter and does not return anything. We can invoke it directly using the class name. It accepts an array of type int, float, double, long, char, byte.
Syntax:
public static void sort(int[] a)
Where a is an array to be short.
Note: Like the Arrays class, the Collections class also provides the sort() method to sort the array. But there is a difference between them. The sort() method of the Arrays class works for primitive type while the sort() method of the Collections class works for objects Collections, such as LinkedList, ArrayList, etc.
SortArrayExample1.java
Output:
In the above program, we can also use the toSting() method of the Arrays class to print the array, as shown in the following statement. It returns a string representation of the specified array.
System.out.printf(Arrays.toString(array));
Without Using the Method
Using the for Loop
In the following example, we have initialized an array of integer type and sort the array in ascending order.
SortArrayExample2.java
Output:
Using the User Defined Method
In the following example, we have defined a method named sortArray() that contains the logic to sort an array in natural order.
SortArrayExample3.java
Output:
Sort Array in Descending Order
The descending order arranges the elements in the highest to lowest order. We can perform sorting in the following ways:
Using the reverseOrder() Method
Java Collections class provides the reverseOrder() method to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a comparator that imposes the reverse of the natural ordering (ascending order).
It means that the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order.
Syntax:
public static <T> Comparator<T> reverseOrder()
Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:
Arrays.sort(a, Collections.reverseOrder());
Let's sorts an array in the descending order.
In the following program, a point to be noticed that we have defined an array as Integer. Because the reverseOrder() method does not work for the primitive data type.
SortArrayExample4.java
Output:
Let's see another program that sorts array elements in alphabetical order.
SortArrayExample5.java
Output:
Without Using the Method
Using the for Loop
In the following example, we have initialized an integer array and perform sorting in descending order.
SortArrayExample6.java
Output:
Using the User Defined Method
SortArrayExample7.java
Output:
How to Sort Subarray
An array derived from the array is known as subarray. Suppose, a[] is an array having the elements [12, 90, 34, 2, 45, 3, 22, 18, 5, 78] and we want to sort array elements from 34 to 18. It will sort the subarray [34, 2, 45, 3, 22, 18] and keep the other elements as it is.
To sort the subarray, the Arrays class provides the static method named sort(). It sorts the specified range of the array into ascending order. We can also sort the array of type long, double, float, char, byte, etc.
Syntax:
public static void sort(int[] a, int fromIndex, int toIndex)
The method parses the following three parameters:
If formIndex is equal to the toIndex, the range to be sorted is empty. It throws IllegalArgumentException if fomIndex is greater than toIndex. It also throws ArrayIndexOutOfBoundsException if fromIndex < 0 or toIndex > a.length.
Let's sort a subarray through a Java program.
SortSubarrayExample.java
Output: