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.

In java, to sort string array we need to match each element of an array to the remaining elements.

Also read – java sort strings alphabetically

Then, if result is > 0 we have to swap them.

Let’s see java program to sort strings array using nested for loop.

In this nested for loop, inner loop starts with a + 1, where “a” is the variable of “outer for loop”.

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.

Also read – Arrays.sort() method in java

Here’s the java program to sort an array using Arrays.sort() method.

import java.util.Arrays;
public class JavaArraySortMethod
{
   public static void main(String[] args)
   {
      String[] strGiven = {"Great Barrier Reef", "Paris", "borabora", "Florence","tokyo", "Cusco"};
      Arrays.sort(strGiven);
      System.out.println("Output(case sensitive) : " + Arrays.toString(strGiven));
   }
}


Output:

Output(case sensitive) : [Cusco, Florence, Great Barrier Reef, Paris, borabora, tokyo]


Also read – preface to java virtual machine and architecture


sort string array alphabetically

Let’s learn java sort string array alphabetically. To sort string array alphabetically first user enters number of strings to sort as input using nextInt() method of Scanner class.

Then using for loop we compare two strings using String.compareTo() method.

Also read – how to create directory in java

String.compareTo() method compares two strings lexicographically.

The comparison is based on the Unicode value of each character in the strings.

compareTo() method returns

  • the value 0 if the argument string is equal to this string.
  • a value less than 0 if this stringis lexicographically less than the string argument.
  • value greater than 0 if this string is lexicographically greater than the string argument.

Let’s see java program to sort strings array in alphabetical order.

import java.util.Arrays;
import java.util.Scanner;
public class SortStringArrayAlphabetically
{
   public static void main(String[] args) 
   {
      int number;
      String temp;
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter number of strings to sort: ");
      number = sc.nextInt();
      String[] strNumber = new String[number];
      Scanner sc2 = new Scanner(System.in);
      System.out.println("Please enter strings one by one to sort: ");
      for(int a = 0; a < number; a++)
      {
         strNumber[a] = sc2.nextLine();
      }
      // here we are sorting strings
      for(int a = 0; a < number; a++)
      {
         for(int b = a + 1; b < number; b++)
         {
            if(strNumber[a].compareTo(strNumber[b]) > 0) 
            {
               temp = strNumber[a];
               strNumber[a] = strNumber[b];
               strNumber[b] = temp;
            }
         }
      }
      System.out.println("Sort string array alphabetically: " + Arrays.toString(strNumber));
      sc.close();
      sc2.close();
   }
}


Output:

Please enter number of strings to sort: 4
Please enter strings one by one to sort:
ajinkya
virat
dhoni
bhuvaneshwar
Sort string array alphabetically: [ajinkya, bhuvaneshwar, dhoni, virat]

Please enter number of strings to sort: 5
Please enter strings one by one to sort:
bharath
vijay
abhay
chethan
jayanth
Sort string array alphabetically: [abhay, bharath, chethan, jayanth, vijay]


Sort string in descending order

Now let’s learn sort string in descending order java.

Let’s see How to sort an array of strings in descending order in java.

import java.util.Arrays;
public class SortStringDescending
{
   public static void main(String[] args)
   {
      char[] chInput = "flowerbrackets".toCharArray();
      descending(chInput);
      System.out.println(String.valueOf(chInput));
   }
   static void descending(char[] ch)
   {
      Arrays.sort(ch);
      descendingOrder(ch);
   }
   static void descendingOrder(char[] chArray)
   {
      int size = chArray.length;
      char ch;
      for(int a = 0; a < size / 2; a++)
      {
         ch = chArray[a];
         chArray[a] = chArray[size - a - 1];
         chArray[size - a - 1] = ch;
      }
   }
}


Output:

wtsrrolkfeecba


Also read – java sort array descending

Also read – arrange sentence in alphabetical order in java


java sort string characters alphabetically

Let’s learn java sort string characters alphabetically using sort() method of Arrays class.

Here’s the java code to arrange the letters of a word in alphabetical order.

import java.util.Arrays;
public class CharactersAlphabetically
{
   public static void main(String[] args)
   {
      String strInput = "flowerbrackets";
      // converting string to char array
      char[] ch = strInput.toCharArray();
      // sorting char array
      Arrays.sort(ch);
      // converting char array to string
      String strSorted = String.valueOf(ch);
      System.out.println("java sort string characters alphabetically: " + strSorted);
   }
}


Output:

java sort string characters alphabetically: abceefklorrstw