Java Program To Sort an Array in Alphabetical Order
In this tutorial, we will learn how to sort the elements of an array in alphabetical order. Sorting refers to arranging data in order either alphabetically or numerically. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Input: a n m w r t s p
Output: a m n p r s t w
The above problem can be solved in the following ways:
Approach 1: Using compareTo()
Approach 2: Using Arrays.sort()
Approach 3: Using reverseOrder()
Let us look at each of these methods separately.
Program 1: Sort an Array in Alphabetical Order
In this approach, we will sort an array in alphabetical order by comparing each element with the rest elements.
Algorithm
- Start
- Declare an Array
- Initialize the Array
- Use two for loops to sort the array in alphabetical order.
- Use the first for loop to hold the elements.
- Use the second for loop to compare with the remaining elements.
- Use the compareTo() to compare.
- Swap the array elements.
- Print the updated array.
- Stop
Below is the code for the same.
The below program demonstrates how to sort an Array in Alphabetical Order by using compareTo()
method.
//Java Program to sort an array in alphabetical order.
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n; //Declare the array size
System.out.println("Enter the number of elements ");
n=sc.nextInt(); //Initialize the array size
String fruits[]=new String[n]; //Declare the array
System.out.println("Enter the String ");
Scanner sc1=new Scanner(System.in);
for(int i=0; i<n ;i++) //Initialize the array
{
fruits[i]=sc1.nextLine();
}
//logic for sorting
for(int i = 0; i<n; i++) //Holds each element
{
for (int j = i+1; j<n; j++) //Check for remaining elements
{
//compares each elements of the array to all the remaining elements
if(fruits[i].compareTo(fruits[j])>0)
{
//swapping array elements
String temp = fruits[i];
fruits[i] = fruits[j];
fruits[j] = temp;
}
}
}
//prints the sorted array in alphabetical order
System.out.println(Arrays.toString(fruits));
}
}
Enter the number of array elements: 10
Enter the array elements:
Apple
Custard Apple
Banana
Kiwi
Guava
Orange
Papaya
Blackberry
Dates
Grapes
[Apple, Banana, Blackberry, Custard apple, Dates, Grapes, Guava, Kiwi, Orange, Papaya , ]
Program 2: Sort an Array in Alphabetical Order
In this approach, we will sort an array in alphabetical order using Arrays.sort() method.
Algorithm
- Start
- Declare an Array
- Initialize the Array
- Call the
Arrays.sort()
function to sort the array in alphabetical order.
- Print the sorted array.
- Stop.
Below is the code for the same.
The below program demonstrates how to sort an Array in Alphabetical Order by using Arrays.sort()
method.
//Java Program to sort an array in alphabetical order.
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the number of elements ");
n=sc.nextInt(); //Initialize array size
String str[]=new String[n]; //Declare array
System.out.println("Enter the String ");
for(int i=0; i<n ;i++) //Initialize array
{
str[i]=sc1.nextLine();
}
Arrays.sort(str); //Sort the array in alphabetical order
System.out.println(Arrays.toString(str)); //Display the array
}
}
Enter the number of elements
5
Enter the String
france
india
china
germany
italy
[china, france, germany, india, italy]
Program 3: Sort an Array in Alphabetical Order
In this approach, we will sort an array in alphabetical order using Arrays.sort()
and then again sort it in reverse order using reverseOrder()
method.
Algorithm
- Start
- Declare an Array
- Initialize the Array
- Call the
Arrays.sort()
function to sort the array in alphabetical order.
- Then call the
reverseOrder()
to sort the array in reverse order.
- Print the sorted array.
- Stop.
Below is the code for the same.
Explanation: The below program demonstrates how to sort an Array in reverse Alphabetical Order using reverseOrder()
method.
/*Java Program to sort an array alphabetically in reverse order*/
import java.util.Arrays;
import java.util.Scanner;
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Scanner sc1=new Scanner(System.in);
int n; //Declare array size
System.out.println("Enter the number of elements ");
n=sc.nextInt(); //Initialize array size
String str[]=new String[n]; //Declare array
System.out.println("Enter the String ");
for(int i=0; i<n ;i++) //Initialize array
{
str[i]=sc1.nextLine();
}
Arrays.sort(str,Collections.reverseOrder()); //Sort the array in alphabetical order
System.out.println(Arrays.toString(str)); //Display the array
}
}
Enter the number of elements
5
Enter the String
Mumbai
Pune
Chennai
Ranchi
Kolkata
[Ranchi, Pune, Mumbai ,Kolkata, Chennai]