Sort array in Java is a method defined in the java.util.Arrays class. As the name indicates, it is useful in sorting an array in ascending or descending order. In this tutorial, you will look at what an arrays.sort() is, and how to use sort array in Java.
What is Arrays.sort() in Java?
The Arrays are a class from the java.util package that provides the pre-defined sort() method. It is a static method that returns no value. When you invoke the sort array in Java, it parses through each element of an array and sorts it in ascending or descending order as specified. The array can be of various data types, including int, long, char, and float. The general syntax of the Arrays.sort() method is:
public static void sort(int ar, int from_index, int to_index)
In the above syntax:
- ar: It is short for the array name
- from_index: An optional parameter that marks the index (inclusive) of the element where the sorting begins
- to_index: An optional parameter that denotes the index (exclusive) of the element where the sorting ends
Since you now see what the Arrays.sort() method is, let’s look at an example to illustrate how to sort an integer array in ascending order and understand this concept better.
Example: Sort an Array in Java in Ascending Order
In the example below, you must define an array named ar with ten integer values. Then, you should use the Arrays.sort() method to sort it. That’s how you can sort an array in Java in ascending order using the Arrays.sort() method.
What Are the Other Ways to Sort Arrays?
Like sorting an array in ascending order, you can also sort it in other ways and types, such as sorting integer array in descending order, a sub-array, and strings in alphabetical order.
Example: Sort the Array in Java in Descending Order
To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class. The reverseOrder() method does not parse the array. Instead, it will merely reverse the natural ordering of the array. It means that you will first use the sort array in Java to sort it in ascending order and then reverse it with the reverseOrder() method. One more thing worth noting is that the Collections.reverseOrder() does not support primitive type. Hence, you must use “Integer” instead of “int” to define the integer data type array. You can see it in action in the below example, where you should use the same integer array you used previously and sort it in descending order.
Example: Sort Subarray in Java
Subarray is nothing but a part of a bigger array. For instance, if you take your ar array from the previous examples, it contains ten values. If you sort only a few elements instead of the entire array, it is called sorting a subarray. This can be done using the full syntax of the sort() method and providing the optional parameters from_index and to_index. When you sort a subarray, the rest of the elements remain unchanged. The below example uses the same ar array and sorts the subarray of the values from index 1 to 7 while keeping the values at index 0, 8, and 9 unchanged.
Example: Sort an Array of Strings in Java in Alphabetical Order
You can also use the sort array in Java to sort strings in alphabetical order. The below example defines a string array and sorts it in both ascending and descending order.
Example: Sort Array in Java Without Using the Sort() Method
You can also sort an array in Java with user-defined methods using the comparator interface and for a loop. All you have to do is define the logic in the method such that it will sort the array. Please look at the below example, where you will have to sort an array without using the Arrays.sort() method in Java.
What Is the Difference Between array.sort() And collection.sort()?
Similar to the Arrays class, the Collections class also provides the sort() method. However, there’s an enormous difference between both methods. The Arrays.sort() method supports primitive data types. On the other hand, the Collections.sort() method supports collections like ArrayList and LinkedList.
Using For Loops
If you have smaller arrays, you can use for loops to perform sorting. Note, however, that if the array size increases, it complicates matters. You can use two for loops, one traversing the array from the starting, and the second for loop, embedded in the outer one, for traversing the next element.
for (initialExpression; testExpression; updateExpression)
// body of the loop
Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.
This tutorial should help resolve any confusion about the ‘sort array’ factor in Java. At the end of the day, we see that sorting arrays is really a simple concept in Java. You can learn about other such simple concepts by referring to Simplilearn’s Java tutorial for beginners. But if you want to excel in Java software development and boost your skills to the next level, we recommend you take an Online Java Certification Course. The course provides 60 hours of applied learning and hands-on coding experience in many frameworks, helping you to get to the very core of Java programming.
Do you have any questions for us regarding Sort Array in Java? Leave them in the comments section below, and our experts will get back to you as soon as they can.