Javatpoint Logo
Javatpoint Logo

How to Sort an Array in Java

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:

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.

Let's sort an array using the sort() method of the Arrays class.

In the following program, we have defined an array of type integer. After that, we have invoked the sort() method of the Arrays class and parses the array to be sort. For printing the sorted array, we have used for loop.

SortArrayExample1.java

Output:

Array elements in ascending order: 
5 
12 
22 
23 
34 
67 
90 
109

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.

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:

Array elements after sorting:
-65
-4
-1
1
3
6
20
34
34
55
78
90

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:

Array elements before sorting: 
12  
45  
1  
-1  
0  
4  
56  
23  
89  
-21  
56  
27  
Array elements after sorting: 
-21  
-1  
0  
1  
4  
12  
23  
27  
45  
56  
56  
89

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
  • Without using the method
    • Using the for Loop
    • Using the User Defined Method

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:

Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:

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:

Array elements in descending order: [205, 110, 102, 78, 23, 11, 6, 4, 0, -1, -9]

Let's see another program that sorts array elements in alphabetical order.

SortArrayExample5.java

Output:

Array elements in descending order: [Papaya, Pineapple, Orange, Mango, Grapes, Banana, Apple]

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:

Array elements in descending order:
94
56
43
32
12
9
5
2
-2
-26
-78

Using the User Defined Method

SortArrayExample7.java

Output:

Enter the number of elements: 7
Enter the elements of the array: 
12
5
56
-2
32
2
-26
Array elements in descending order:
56
32
12
5
2
-2
-26

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:

The method parses the following three parameters:

  • a: An array to be sort.
  • fromIndex: The index of the first element of the subarray. It participates in the sorting.
  • toIndex: The index of the last element of the subarray. It does not participate in the sorting.

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:

Sorted Subarray: 
12
90
2
3
22
34
45
18
5
78

Next TopicJava Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA