What Is Sort Array in Java: Everything You Need to Know

Sort array in Java is a method defined in the java.util.Arrays class. As the name gives out, it is useful in sorting an array in ascending or descending order. In this tutorial, you will look at what is arrays.sort() 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 know what the Arrays.sort() method is, look at an example 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 have to 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.

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

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.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

This tutorial would have cleared all your doubts about the ‘sort array’ factor in Java. Sorting arrays is 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, it is recommended to 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.

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 on the same, soon.

Happy learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.