The sort array in Java is a method defined in the java.util.Arrays class. As the name suggests, it is useful for sorting an array in ascending or descending order. In this tutorial, you will learn what arrays.sort () is and how to use a sort array in Java.

Full stack Java developer course

In partnership with HIRIST and HackerEarthSTUDY THE COURSE

What is Arrays.sort () in Java?

Arrays are a class in the java.util package that provides the predefined sort () method. This is a static method that does not return a value. When you call a sort array in Java, it analyzes each element of the array and sorts it in ascending or descending order, as specified. The array can be of different data types, including int, long, char and float. The general syntax of the Arrays.sort () method is:

public static invalid sort (int[] ar, int from_index, int to_index)

In the above syntax:

  • ar: This is short for array name
  • from_index: Optional parameter that marks the index (inclusive) of the item from which sorting starts
  • to_index: Optional parameter that indicates the index (exclusive) of the item where 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 better understand this concept.

Example: Sort an array in Java in ascending order

In the example below, you need to define an array named ar with ten integers. Then you need to use the Arrays.sort () method to sort it. Here’s how you can sort an array in Java in ascending order using the Arrays.sort () method.

Javaina ascending order


FREE Java certification training

Learn AZ from Java like never beforeSign up now

FREE Java certification training

What are the other ways to sort arrays?

Similar to sorting an array in ascending order, you can also sort it in other ways and types, such as sorting an integer array in descending order, subarray, and strings alphabetically.

Example: Sort the array in Java in descending order

To sort an array in Java in descending order, you must use the reverseOrder () method of the Collections class. The reverseOrder () method does not parse an array. Instead, it will simply reverse the natural arrangement of the array. This means that you will first use the sort array in Java to sort it in ascending order, and then invert it with the reverseOrder () method. Another thing worth noting is that Collections.reverseOrder () does not support a primitive type. Therefore, you must use “Integer” instead of “int” to define an array of integer data types. You can see it in action in the example below, where you need to use the same integer array you used before and sort it in descending order.

Java in descending order.


Example: Sorting a subarray in Java

A subarray is nothing but part of a larger array. For example, if you take your array from the previous examples, it contains ten values. If you sort only a few elements instead of the whole array, this is called subarray sorting. 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 other elements remain unchanged. The example below uses the same array and sorts the subarray from values ​​from index 1 to 7, keeping the values ​​from index 0, 8 and 9 unchanged.

submass in Java submass in Java_rslt

Full Stack course for web developers

To become an expert in MEAN StackSee the course

Full Stack course for web developers

Example: Sort an array of strings in Java alphabetically

You can also use the sort array in Java to sort strings alphabetically. The example below defines an array of strings and sorts it in ascending and descending order.

Javanese alphabetical order


Example: Sorting an array in Java without using the Sort () method.

You can also sort an array in Java with user-defined methods using the comparison and loop interfaces. All you have to do is define the logic in the method so that it sorts the array. Please see the example below where you will need to sort an array without using the Arrays.sort () method in Java.

Sort in Java

Sort in Java_rslt

What is the difference between array.sort () and collection.sort ()?

Like the Arrays class, the Collections class also provides the sort () method. However, there is a huge difference between the two methods. The Arrays.sort () method supports primitive data types. On the other hand, the Collections.sort () method supports collections such as ArrayList and LinkedList.

Using For Loops

If you have smaller arrays, you can use for loops to perform sorting. Keep in mind, however, that if the size of the array increases, it complicates matters. You can use two for loops, one going through the array from the beginning and the other for the built-in for loop going through the next element.

for (initialExpression; testExpression; updateExpression) {

// body of the loop



Get a solid 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 Java sorting factor. At the end of the day, we see that sorting arrays is a really simple concept in Java. You can learn about other such simple concepts by referring to the Simplilearn Java tutorial for beginners. But if you want to excel in Java software development and take your skills to the next level, we recommend that you take an online Java certification course. The course provides 60 hours of applied training and practical experience in multi-frame coding, helping you get to the very core of Java programming.

Do you have questions for us about sorting an array in Java? Leave them in the comments section below and our experts will contact you as soon as possible.

Enjoyable learning!

Previous articleRed Hat rejects the CodeReady name for developer environments
Next articleTrends in health care payments Annual report: Summary