How to Sort String Array in Java

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 in the standard form) data and for producing a human-readable format. In this section, we will learn how to sort String array in Java using user-defined logic and Arrays. sort() method

There are two ways to sort a string array in Java:

  • Using User-Defined Logic
  • Using the Arrays.sort() Methodm

Using User Defined Logic

We can sort a string array by comparing each element with the rest 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 than 0, it performs the swapping and sorts the array.

SortStringArrayExample1.java

Output:

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

Using the Arrays.sort() Method

In Java, Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order. It uses 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 type int, float, double, long, char, 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 sort() method of the Arrays class works for primitive type while the sort() method of the Collections class works for objects Collections, such as LinkedList, ArrayList, etc.

We can perform sorting in the following ways:

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

Sort String Array in Ascending Order or Alphabetical Order

The ascending order arranges the elements in the lowest to highest order. It is also known as natural order or alphabetical order.

Let's sort an array using the sort() method of the Arrays class.

SortStringArrayExample2.java

Output:

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

Sort String Array in Descending Order or Reverse Natural Order

Using the reverseOrder() Method

Java Collections class provides the reverseOrder() method 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 the array sorts elements in the ascending order by using the sort() method, after that the reverseOrder() method reverses the natural ordering, and we get the sorted array in descending order.

Syntax:

Suppose, a[] is an array to be sort in the descending order. We will use the reverseOrder() method in the following way:

Let's sort a string array in the descending order.

SortStringArrayExample3.java

Output:

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

Next TopicJava Tutorial