Arrays.sort() in Java14 May 2025 | 7 min read In Java, sort() method is a static and overloaded method defined in the java.util.Arrays class. It is used to sort arrays values of different types. It is widely used to organize data in ascending order. The sort() method can be applied with primitive types (int, char, double, float, etc.) and object type (String, Integer, Double, etc.). Note that the method uses a dual-pivot Quicksort algorithm for primitive types and a modified merge sort for object arrays that ensures efficient sorting. Each sort() method provides the following two variants:
Examples of Arrays.sort() MethodSorting an Array of Primitive Data TypesExampleCompile and RunOutput: Sorted Integer Array: 1, 2, 3, 5, 8, 11, 12, 14, 17 Sorted Float Array: 1.05, 1.2, 2.2, 3.89, 5.4, 5.6, 6.7 Sorted Double Array: 1.5, 2.7, 3.2, 4.8, 5.1, 6.8 Sorted Character Array: a, b, b, c, d, k, l, y Example: Sorting a SubarrayExampleCompile and RunOutput: Sorted Integer Array: 12, 8, 5, 1, 3, 11, 14, 17, 2 Sorted Float Array: 1.2, 6.7, 1.05, 2.2, 5.4, 5.6, 3.89 Sorted Double Array: 1.5, 6.8, 2.7, 3.2, 4.8, 5.1 Sorted Character Array: b, a, b, c, d, l, y, k Similarly, we can sort short and byte type array. Example: Sorting a StringExampleCompile and RunOutput: [Australia, Austria, London, Paris, South Africa] Example: Custom Sorting with ComparatorExampleCompile and RunOutput: 912 Tom New York 917 Peter Amsterdam 1024 Jack Las Vegas Example: Sorting Comparable InterfaceExampleCompile and RunOutput: 912 Tom New York 917 Peter Amsterdam 1024 Jack Las Vegas Important Points to Remember
ConclusionThe Java Arrays.sort() method is an enormously powerful and versatile tool that not only sorts primitive arrays but does it for object arrays as well. Its simple syntax and overloaded methods make it possible for developers to perform anything from this one-dimensional basic numeric sorting to an advanced custom logic with comparators. It well sustains a wide universe of use case scenarios through optimized ones such as dual-pivot Quicksort for primitives. Whether you are creating small utilities or more complex applications, you will find Arrays.sort() a very beneficial routine for managing data, writing cleaner code, and developing software with a better design. Java Arrays.sort() MCQs1. Which method we should use to sort a subarray of integer type?
Answer: 3) Explanation: When we want to sort the specified range of the array into ascending order we use Arrays.sort(int[] a, int fromIndex, int toIndex) 2. Which algorithm is used by the sort() method to sort primitive type?
Answer: 1) Explanation: The sort() method uses a dual-pivot Quicksort algorithm for primitive types 3. Which algorithm is used by the sort() method to sort object types?
Answer: 1) Explanation: The sort() method uses a Modified Merge Sort algorithm for object type arrays. 4. In which class the sort() method is defined?
Answer: 2) Explanation: The sort() method is defined in the java.util.Arrays class. 5. The sort() method is a _______ method of the Arrays class?
Answer: 2) Explanation: The sort() method is a static and overloaded of the Arrays class. Next TopicJava Set to List |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India