How to Sort a String Array in Java?Last Updated : 7 Jan 2026 In programming, sorting is important because it puts elements of an array in a certain order. The widely used order is alphabetical order or natural order. The sorting is used for canonicalizing (the process of converting data into the standard form) data and for producing a human-readable format. To read more String Array in Java There are two ways to sort a string array in Java:
Using User-Defined LogicWe can sort a string array by comparing each element with the other elements. In the following example, we have done the same. We have used two for loops. The inner (second) for loop avoids the repetitions in comparison. If the condition (countries[i].compareTo(countries[j])>0) is true 0, it performs the swapping and sorts the array. Example: Sorting String ArrayCompile and RunOutput: [ Australia, America, Denmark, France, Germany, India, Italy, Netherlands, South-Africa, Yugoslavia, Zimbabwe] Using the Arrays.sort() MethodIn Java, Arrays.sort() method is used to sort an array in ascending order. It uses the Dual-Pivot Quicksort algorithm for sorting. Its complexity is O(n log(n)). It is a static method that parses an array as a parameter and does not return anything. We can invoke it directly by using the class name. It accepts an array of types: int, float, double, long, char, and byte. Syntax:Where a is an array to be short. Note: Like the Arrays class, the Collections class also provides the sort() method to sort the array. But there is a difference between them. The Arrays.sort() method works for primitive types, while the Collections.sort() method works for object type, such as LinkedList, ArrayList, etc.We can perform sorting in the following ways:
Sorting String Array in Ascending Order or Alphabetical OrderThe ascending order arranges the elements from the lowest to the highest order. It is also known as natural order or alphabetical order. Example: Sorting String Array Using Arrays.sort() Method ExampleCompile and RunOutput: [Apple, Apricot, Blackberry, Custard apple, Date, Fig, Mulberry, Naseberry, Orange, Plum, Tamarind, Wood apple] Sorting String Array in Descending Order or Reverse Natural OrderUsing the Collections.reverseOrder() Method Java Collections.reverseOrder() method is used to sort the array in reverse-lexicographic order. It is a static method, so we can invoke it directly by using the class name. It does not parse any parameter. It returns a comparator that imposes the reverse of the natural ordering (ascending order). It means that first Arrays.sort() method sorts elements in ascending order after that the Collections.reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order. Syntax: Suppose a[] is an array to be sorted in descending order. We will use the Collections.reverseOrder() method in the following way: Example: Sorting element in Descending Order ExampleCompile and RunOutput: [Zimbabwe, Yugoslavia, South-Africa, Netherlands, Italy, India, Germany, France, Denmark, America, Australia] Next TopicJava Tutorial |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India