Sort string in alphabetical order java: In the previous article, we have seen Java Program to Find the Union of Multiple Arrays
In this article we are going to see how to sort string elements in alphabetical order using Java programming language.
Java Program to Sort String Elements in an Alphabetical Order
How to sort strings in alphabetical order in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to sort string elements in an alphabetical order in an array.
Method-1: Java Program to Sort String Elements in an Alphabetical Order By Using compareTo() Method
Approach:
- Run two nested loops one
i = 0 to arr.length
, second onej = i+1 to arr.length
. - Using
CompareTo()
method compare one string with the rest of the strings. - If it returns > 0 then the parameter passed to
compareTo()
method is lexicographically first, so swap the elements. - Else the elements are lexicographically correct.
- Print the Sorted elements in an Alphabetical Order.
Program:
public class Main { public static void main(String[] args) { // initialize the array String[] words = { "apple", "dog", "boy", "cat" }; // call the method sortAlphabetically(words); } static void sortAlphabetically(String[] words) { String temp; for (int i = 0; i < words.length; i++) { for (int j = i + 1; j < words.length; j++) { // to compare one string with other strings if (words[i].compareTo(words[j]) > 0) { // swapping temp = words[i]; words[i] = words[j]; words[j] = temp; } } } // print output array System.out.print("The words in alphabetical order are: "); for (int i = 0; i < words.length; i++) { System.out.print(words[i] + " "); } } }
Output: The words in alphabetical order are: apple boy cat dog
Method-2: Java Program to Sort String Elements in an Alphabetical Order By Using Arrays.sort() Method
Approach:
- Create scanner class object.
- Ask the user for number of arrays.
- Ask the user to enter the size of each individual list and the its elements one by one.
- Sort the array using
Arrays.sort()
method. - Print the sorted array.
Program:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the number of words: "); int n = sc.nextInt(); // extra sc.nextLine(); to consume the new line character sc.nextLine(); // initialize array with size n String[] words = new String[n]; // take input from user for array elements System.out.println("Enter the words: "); for (int i = 0; i < n; i++) { words[i] = sc.nextLine(); } sortAlphabetically(words); } static void sortAlphabetically(String[] words) { // sorting the array Arrays.sort(words); System.out.print("The words in alphabetical order are: " + Arrays.toString(words)); } }
Output: Enter the number of words: 6 Enter the words: dog ant cat bat fan ear The words in alphabetical order are: [ant, bat, cat, dog, ear, fan]
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs:
- Java Program to Increment Each Element of Array by 1 and Print the Incremented Array
- Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array
- Java Program to Decrement Each Element of Array by a Specified Number
- Java Program to Increment Each Element of Array by a Specified Number