➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
How to Sort ArrayList of Strings Alphabetically in Java? In this section, we will discuss how to sort ArrayList of strings alphabetically in Java. Let us understand it through an example:-
For example:-
list = [“Java”, “Python”, “HTML”, “C++”, “CSS”]
list after sorting alphabetically:- [C++, CSS, HTML, Java, Python]
To sort ArrayList of strings alphabetically in Java we can take help of Collections class sort() method. When we call Collections.sort() method for ArrayList of strings then by default it will sort them in ascending order of alphabets i.e. in alphabetical order.
Program to Sort ArrayList of Strings Alphabetically in Java
Let us see how to sort strings alphabetically in Java. We will follow the following steps:-
Step1: First we will import the needed libraries that are ArrayList, Collections, and Iterator.
Step2: Create a main method in the main class and also create an ArrayList method and add elements to the list by using arrayList.add() and then use the sort() method in the collections framework to sort the array list of strings.
Step3: Display the output i.e. both the list before sorting and after sorting.
Program to Sort ArrayList of Strings Alphabetically in Java
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Zen");
names.add("Fishley");
names.add("John");
names.add("Kernel");
names.add("Manya");
names.add("Lohith");
names.add("Henry");
names.add("Albert");
names.add("Cubbon");
names.add("Donald");
System.out.println("ArrayList before sorting: " + names);
Collections.sort(names);
System.out.println("ArrayList after sorting: " + names);
}
}
Output:-
ArrayList before sorting: [Zen, Fishley, John, Kernel, Manya, Lohith, Henry, Albert, Cubbon, Donald]
ArrayList after sorting: [Albert, Cubbon, Donald, Fishley, Henry, John, Kernel, Lohith, Manya, Zen]
Program to Sort ArrayList of Strings Alphabetically Reverse in Java
In the above program, we have seen how to sort ArrayList of strings alphabetically in Java. Now let us see how to sort ArrayList of strings in reverse alphabetical order.
To sort in reverse order we can take the help of the sort() and reverseOrder() method given in the Collections class. The reverseOrder() method returns a comparator which is passed to the sort() method as a second parameter.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Zen");
names.add("Fishley");
names.add("John");
names.add("Kernel");
names.add("Manya");
names.add("Lohith");
names.add("Henry");
names.add("Albert");
names.add("Cubbon");
names.add("Donald");
System.out.println("ArrayList before sorting: "
+ names);
Collections.sort(names, Collections.reverseOrder());
System.out.println("ArrayList after sorting in "+
"reverse order: " + names);
}
}
Output:-
ArrayList before sorting: [Zen, Fishley, John, Kernel, Manya, Lohith, Henry, Albert, Cubbon, Donald]
ArrayList after sorting in reverse order: [Zen, Manya, Lohith, Kernel, John, Henry, Fishley, Donald, Cubbon, Albert]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!