How to sort a string array in java

Let’s learn how to sort a string array in java.

How to sort a string array in java

Basically String class do not have method to sort string array.

So to sort string array we need to match each element of an array to the remaining elements.

To do that we are using nested for loop. Inner for loop avoid duplicates in comparison and starts with a + 1, where “a” is variable of “outer for loop”.

Then, if (strPlaces[a].compareTo(strPlaces[b]) > 0) is true then swap and sort given string array.

In the below example let’s learn how to sort string array without using sort method.

// how to sort string array in java without using sort method
import java.util.Arrays;
public class SortStringArray
{
   public static void main(String[] args)
   {
      String[] strPlaces = {"Great Barrier Reef", "Paris", "BoraBora", "Florence","Tokyo", "Cusco"};
      int size = strPlaces.length;
      for(int a = 0; a < size - 1; a++)
      {
         for(int b = a + 1; b < strPlaces.length; b++)
         {
            if(strPlaces[a].compareTo(strPlaces[b]) > 0)
            {
               String temp = strPlaces[a];
               strPlaces[a] = strPlaces[b];
               strPlaces[b] = temp;
            }
         }
      }
      System.out.println(Arrays.toString(strPlaces));
   }
}


Output:

how to sort a string array in java

Using Arrays.sort() method

Arrays.sort() method sorts the specified array of objects into ascending order according to the natural ordering of its elements.

Here’s the java program to sort string array in ascending order or alphabetical order using Arrays.sort() method.

import java.util.Arrays;
public class SortStringArrayAlphabeticalOrder
{
   public static void main(String[] args)
   {
      String[] strAsc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
      System.out.println("Before sorting: ");
      for(String string : strAsc)
      {
         System.out.println(string);
      }
      // arrays.sort
      Arrays.sort(strAsc);
      System.out.println("Sorting string array alphabetically: ");
      for(String str : strAsc)
      {
         System.out.println(str);
      }
   }
}


Output:

Before sorting:
Bear
Fox
Deer
Cheetah
Anteater
Elephant
Sorting string array alphabetically:
Anteater
Bear
Cheetah
Deer
Elephant
Fox


Sort string array in descending order or reverse natural order

Similarly we can sort above string array in reverse order using reverseOrder() method of Collections class.

reverseOrder() method sorts the array in reverse-lexicographic (alphabetical) order. The returned comparator is serializable.

Here’s the java program on how to sort an array of strings in descending or reverse order in java.

import java.util.Arrays;
import java.util.Collections;
public class SortStringArrayDescendingOrder
{
   public static void main(String[] args)
   {
      String[] strDesc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
      System.out.println("Before sorting: ");
      for(String string : strDesc)
      {
         System.out.println(string);
      }
      // arrays.sort
      Arrays.sort(strDesc, Collections.reverseOrder());
      System.out.println("Sorting string array in descending or reverse order: ");
      for(String string : strDesc)
      {
         System.out.println(string);
      }
   }
}


Output:

Before sorting:
Bear
Fox
Deer
Cheetah
Anteater
Elephant
Sorting string array in descending or reverse order:
Fox
Elephant
Deer
Cheetah
Bear
Anteater