BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl
Home / Java Examples / Java Program to Sort Strings in an Alphabetical Order

Java Program to Sort Strings in an Alphabetical Order

By Chaitanya Singh

In this java tutorial, we will learn how to sort Strings in an Alphabetical Order.

Java Example: Arranging Strings in an Alphabetical Order

In this program, we are asking user to enter the count of strings that he would like to enter for sorting. Once the count is captured using Scanner class, we have initialized a String array of the input count size and then are running a for loop to capture all the strings input by user.

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

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:
Java Program to Sort Strings in an Alphabetical Order

Related Java examples:
1. Java program to reverse words in a String
2. Java program to calculate and print Student grades
3. Java program to reverse a String using recursion
4. Java program to find duplicate characters in a String

Posted Under: Java Examples

Comments

  1. Srikar Kalivarapu says

    January 4, 2019 at 9:24 AM

    Change
    str[i].compareTo(str[j])>0 to str[i].compareToIgnoreCase(str[j])>0

    Reply
  2. Hilda Ullrich says

    August 29, 2019 at 11:16 AM

    Hello Chaitanya,

    the “Arranging Strings in an Alphabetical Order” program doesn’t treats the issue extensively by comparing the 2 strings with each other, isn’t it? Otherwise what is the compareTo method then for?

    Thank you for our answer,
    Hilda

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap