Java Program to Sort Names in an Alphabetical Order
Last Updated :
15 Sep, 2022
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 their 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 Order
import 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]);
}
}
}
OutputThe 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 Order
import 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]);
}
}
}
OutputThe names in alphabetical order are:
Ajay
Gourav
Rahul
Riya
Time Complexity: O(n log n)
Similar Reads
Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort()
2 min read
Java Program to Sort the Elements of an Array in Descending Order Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing.1. Using Collections.reverseOrder()In this example, we will use Collections.reverseO
2 min read
Java Program to Sort Objects in ArrayList by Date The foremost tool that strikes is the sort() method to be used for the comparator mechanism of the Collections class which sorts in the decreasing order. Yes if in generic we want to achieve the goal considering the boundary condition where objects to sorted are user-defined then blindly do with Com
6 min read
Java Program to Sort LinkedList using Comparable In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure where all the elements are unsorted in contiguous memory locations. The advantage of LinkedList is it is dynamic and easy to insert and delete any element. We can not access
5 min read
Java Program for Menu Driven Sorting of Array In Java, sorting an array consists of arranging the elements in a particular order, such as ascending or descending. This can be achieved using various algorithms like Bubble Sort, Selection Sort, or Insertion Sort. A menu-driven program allows users to select the desired sorting method dynamically.
7 min read
How to Sort an ArrayList of Objects by Property in Java? ArrayList in Java (equivalent to vector in C++) having a dynamic size. It can be shrinked or expanded based on size. ArrayList is a part of the collection framework and is present in java.util package. --> java.util Package --> ArrayList Class Syntax: Creating an empty ArrayList ArrayList <
7 min read