How to Sort Strings in an Alphabetically Order in Java Program ?

  Java Interview Q&A

In this program, we are asking the user to enter a count of strings that he wants to enter for sorting. After capturing the computation using the scanner class, we initialized a string array of input count size and then running for a loop to capture all strings input by the user.

Once the strings are all strings stored in the string, we are comparing the first alphabet of each string to sort them in alphabetical order.

Sorting Strings in an Alphabetical Order in Java

import java.util.Scanner;
public class JavaExample
{
    public static void main(String[] args) 
    {
        int count;
        String temp;
        Scanner scan = new Scanner(System.in);
        
        //User will be asked to enter the count of strings 
        System.out.print("Enter number of strings you would like to enter:");
        count = scan.nextInt();
        
        
        String str[] = new String[count];
        Scanner scan2 = new Scanner(System.in);
        
        //User is entering the strings and they are stored in an array
        System.out.println("Enter the Strings one by one:");
        for(int i = 0; i < count; i++)
        {
            str[i] = scan2.nextLine();
        }
        scan.close();
        scan2.close();
        
        //Sorting the strings
        for (int i = 0; i < count; i++) 
        {
            for (int j = i + 1; j < count; j++) { 
                if (str[i].compareTo(str[j])>0) 
                {
                    temp = str[i];
                    str[i] = str[j];
                    str[j] = temp;
                }
            }
        }
        
        //Displaying the strings after sorting them based on alphabetical order
        System.out.print("Strings in Sorted Order:");
        for (int i = 0; i <= count - 1; i++) 
        {
            System.out.print(str[i] + ", ");
        }
    }
}

Output

Enter number of strings you would like to enter : 5
Rick 
Steve
Abey
Lina 
Manisha
String in Sorted order : Abey, Lina, Manisha, Rick, Steve,

Using the toCharArray() method to Sorting Strings in an Alphabetical Order

TheCharArray () method of this range converts the string into a character array and returns it. To sort a string value alphabetically –

  • Get the required string.
  • Convert a given string into a character array using the ToCharArray () method.
  • Sort the received array using the sort () method of the arrays class.
  • Convert the sorted array into a string by passing it to the constructor of the string array.
import java.util.Arrays;
import java.util.Scanner;
public class SortingString {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string value: ");
      String str = sc.nextLine();
      char charArray[] = str.toCharArray();
      Arrays.sort(charArray);
      System.out.println(new String(charArray));
   }
}

Output :

Enter a string value:
fastread.in
.aaedfinrst

Read Here Complete Java Tutorial – Click Here

LEAVE A COMMENT