Java Program to Sort Names in an Alphabetical Order - GeeksforGeeks

04 Jan, 2021
For, sorting names in an Alphabetical order there are multiple ways to sort the array, like using inbuilt Arrays.sort() method or using normal sorting algorithms like the bubble sort, merge sort. Here let’s use the bubble sort and inbuilt sort.
Example:
Input : Array[] = {"Sourabh", "Anoop, "Harsh", "Alok", "Tanuj"}
Output: Array[] = {"Alok", "Anoop", "Harsh", "Sourabh", "Tanuj"}
 
Input : Array[] = {"Bob", "Alice"}
Output: Array[] = {"Alice", "Bob"}
Approach: Brute-Force Approach
The idea is to compare the strings on the basis of there unicode and swap them in accordance with the returned int value based on the comparison between the two strings using compareTo() method.
In the input, the user has to enter the number of names and the names and on the output, it will sort and display them in alphabetical order. For this, we are going to use the compareTo() method and compare one string with the rest of the strings.
CompareTo() is used to compare two strings lexicographically. Each character of both strings is converted into its unicode value. Lexicographical order is nothing but alphabetical order. 
This method returns an int data-type which is based on the comparison between the two string. If it returns>0 then the parameter passed to compareTo() method is lexicographically first whereas if returns < 0 then string calling the method is lexicographically correct.
Steps
Using CompareTo() method compare one string with the rest of the strings
To swap the elements based on the comparison between the two string.
Print the Sorted Names in an Alphabetical Order.
Below is the implementation of the above approach:
Java
// Java Program to Sort Names in an Alphabetical Orderimport java.io.*;  class GFG {    public static void main(String[] args)    {        // storing input in variable        int n = 4;                // create string array called names        String names[]            = { "Rahul", "Ajay", "Gourav", "Riya" };        String temp;        for (int i = 0; i < n; i++) {            for (int j = i + 1; j < n; j++) {                                // to compare one string with other strings                if (names[i].compareTo(names[j]) > 0) {                    // swapping                    temp = names[i];                    names[i] = names[j];                    names[j] = temp;                }            }        }                // print output array        System.out.println(            "The names in alphabetical order are: ");        for (int i = 0; i < n; i++) {            System.out.println(names[i]);        }    }}
The names in alphabetical order are: 
Ajay
Gourav
Rahul
Riya
Time Complexity: O(N2)
Approach: Inbuilt Sort function
Using inbuilt Arrays.sort() method to sort the array.
Print the Sorted Names in an Alphabetical Order.
Below is the implementation of the above approach:
Java
// Java Program to Sort Names in an Alphabetical Orderimport java.io.*;import java.util.*;  class GFG {    public static void main(String[] args)    {        // storing input in variable        int n = 4;        // create string array called names        String names[]            = { "Rahul", "Ajay", "Gourav", "Riya" };        // inbuilt sort function        Arrays.sort(names);        // print output array        System.out.println(            "The names in alphabetical order are: ");        for (int i = 0; i < n; i++) {            System.out.println(names[i]);        }    }}
The names in alphabetical order are: 
Ajay
Gourav
Rahul
Riya
Time Complexity: O(n log n)
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Convert a String to Character array in Java
Java Programming Examples
Implementing a Linked List in Java using Class
Convert Double to Integer in Java
How to Apply Different Styles to a Cell in a Spreadsheet using Java?