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 Full Stack Program, you can learn more about what an arrays.sort() is, and how to use sort array in Java.

Sorting arrays in Java is fundamental for organizing data efficiently. Whether it's sorting numbers, strings, or custom objects, mastering sorting algorithms is crucial. Enroll in a Java Course to delve into various sorting techniques and elevate your programming proficiency.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Javainascendingorder

Javainascendingorder_rslt.

Preparing Your Blockchain Career for 2024

Free Webinar | 5 Dec, Tuesday | 9 PM ISTRegister Now
Preparing Your Blockchain Career for 2024

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.

Javaindescendingorder.

Javaindescendingorder_rslt

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.

subarrayinJava subarrayinJava_rslt

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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.

Javainalphabeticalorder

Javainalphabeticalorder_rslt

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.

SortarrayinJava

SortarrayinJava_rslt

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

}

Source

Conclusion

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.

If you want to learn more about Java and other software development topics, then check out Simplilearn's Post Graduate Program In Full Stack Web Development, designed with Caltech and is the perfect program to help you accelerate your journey as a software developer. 

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.

Happy learning!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 30 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449