How to sort a string array in java

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

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. Then, if result is > 0 we have to swap them.

Also read – java sort strings alphabetically

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 program we are sorting string array 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 strings array,

import java.util.Arrays;

public class JavaArraySort
{
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


String array alphabetically

Let’s learn java sort string array alphabetically. To sort string array alphabetically first user enters number of strings as input using scanner class.

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

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 SortStringArray 
{
   public static void main(String[] args) 
   {
      int number;
      String temp;
      Scanner sc = new Scanner(System.in); 
      System.out.print("Please enter number of strings to sort: ");
      number = sc.nextInt();
      String[] strNumber = new String[number];
      System.out.println("Please enter strings one by one to sort: ");
      for(int a = 0; a < number; a++)
      {
         strNumber[a] = sc.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.print("Sort string array alphabetically: " + Arrays.toString(strNumber));
      sc.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;
import java.util.Collections;

public class StringArrayDescending 
{
   public static void main(String[] args) 
   {
      String[] players = {"ajinkya", "virat", "dhoni","bhuvaneshwar","jaspreet"};
      Arrays.sort(players, Collections.reverseOrder());
      System.out.println("Sort string in descending order java: ");       
      for(String str : players)
      {
         System.out.println(str);
      }
   }
}



Output:

Sort string in descending order java:
virat
jaspreet
dhoni
bhuvaneshwar
ajinkya