Java program to sort names in an alphabetical order

Let’s learn java program to sort names in an alphabetical order.

Java program to sort names in an alphabetical order

Sort names in alphabetical order is one of the common java interview question. Now let’s learn java sort string array alphabetically.

  • In java sort names alphabetically first user enters number of strings in arranging strings in an alphabetical order.
  • In the next step number of strings is stored in integer variable ‘number’ using nextInt() method Scanner class.
  • Then for loop is used to store all the strings entered by user. Finally compare first letter of each string to store in alphabetical order.

Also read – nested classes in java

Here’s how to sort names alphabetically in java or java program to sort names in an alphabetical order.

import java.util.Scanner;
public class SortNamesAlphabeticalOrder
{
   public static void main(String[] args)
   {
      int number;
      String str;
      Scanner sc1 = new Scanner(System.in);
      System.out.println("Please enter number of strings: ");
      number = sc1.nextInt();
      String[] names = new String[number];
      Scanner sc2 = new Scanner(System.in);
      System.out.println("Enter all strings: ");
      for(int a = 0; a < number; a++)
      {
         names[a] = sc2.nextLine();
      }
      for(int a = 0; a < number; a++)
      {
         for(int b = a + 1; b < number; b++)
         {
            // java alphabetical sort
            if(names[a].compareTo(names[b]) > 0)
            {
               str = names[a];
               names[a] = names[b];
               names[b] = str;
            }
         }
      }
      System.out.println("After sorting names in an alphabetical order: ");
      for(int a = 0; a < number - 1; a++)
      {
         System.out.println(names[a] + ", ");
      }
      System.out.print(names[number - 1]);
      sc1.close();
      sc2.close();
   }
}


Output:

java program to sort names in an alphabetical order

Arrange sentence in alphabetical order in java

We can use collection to arrange sentence in alphabetical order in java.

import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class ArrangeInAlphabeticalOrder 
{
   public static void main(String[] args) 
   {
      Set set = new TreeSet();
      String strInput = "hi all welcome to flower brackets blog";
      System.out.println("Before - arrange sentence in alphabetical order: " + strInput);
      StringTokenizer strToken = new StringTokenizer(strInput," ");
      while(strToken.hasMoreElements())
      {
         set.add(strToken.nextElement());
      }
      System.out.println("After - arrange sentence in alphabetical order: " + set);
   }
}


Output:

Before – arrange sentence in alphabetical order: hi all welcome to flower brackets blog
After – arrange sentence in alphabetical order: [all, blog, brackets, flower, hi, to, welcome]


Write a program which will read a string and rewrite it in the alphabetical order in java

Let’s learn to write a program which will read a string and rewrite it in the alphabetical order in java.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class JavaAlphabeticalOrder 
{
   String rewriteOrder(String strInput)
   {
      char[] ch = strInput.toCharArray();
      Arrays.sort(ch);
      String str = new String(ch);
      return str;
   }
   public static void main(String[] args) throws IOException 
   {
      System.out.println("Please enter a string rewrite it in the alphabetical order: ");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String str = br.readLine();
      JavaAlphabeticalOrder obj = new JavaAlphabeticalOrder();
      String strAlphabetical = obj.rewriteOrder(str);
      System.out.println("String in alphabetic order in java: " + strAlphabetical);
   }
}


Output:

Please enter a string rewrite it in the alphabetical order: hi all welcome to flower brackets blog
String in alphabetic order in java: aabbcceeeefghiklllllmoooorrsttww


How to check if a string is in alphabetical order in java

To check if a string is in alphabetical order or not first store the given string in character array ‘chArray’.

Now this character array is sorted using Arrays.sort() method. In the next step check if characters in sorted array are same as given string.

Also read – java code to arrange the letters of a word in alphabetical order

If they are same then print given string is in alphabetical order else print given string is not in alphabetical order on console.

import java.util.Arrays;
public class CheckIfStringAlphabetical 
{
   public static void main(String[] args) 
   {
      String strInput = "helloworld";
      if(checkIfAlphabaticOrder(strInput))
      {
         System.out.println("Given string is in alphabetical order.");
      }
      else
      {
         System.out.println("Given string is not in alphabetical order.");
      }
   }
   static boolean checkIfAlphabaticOrder(String str)  
   {  
      int size = str.length();  
      char[] chArray = new char[size];  
      for(int a = 0; a < size; a++) 
      {  
         chArray[a] = str.charAt(a);  
      }  
      Arrays.sort(chArray);
      for(int a = 0; a < size; a++)
      {
         if(chArray[a] != str.charAt(a))
         {
            return false;
         }
      }                
      return true;      
   }
}


Output:

Given string is not in alphabetical order.


Also read – sort string in descending order java

Also read – java sort string characters alphabetically