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 Logic
  • Using sort() Method

Using User-Defined Logic

We 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 Array

Compile and Run

Output:

[ Australia, America, Denmark, France, Germany, India, Italy, Netherlands, South-Africa, Yugoslavia, Zimbabwe]

Using the Arrays.sort() Method

In 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:

  • Ascending Order, Alphabetical Order, or Natural Order
  • Descending Orderor Reverse Natural Order

Sorting String Array in Ascending Order or Alphabetical Order

The 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

Example

Compile and Run

Output:

[Apple, Apricot, Blackberry, Custard apple, Date, Fig, Mulberry, Naseberry, Orange, Plum, Tamarind, Wood apple]

Sorting String Array in Descending Order or Reverse Natural Order

Using 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

Example

Compile and Run

Output:

[Zimbabwe, Yugoslavia, South-Africa, Netherlands, Italy, India, Germany, France, Denmark, America, Australia]

Next TopicJava Tutorial