Write a Java program to sort a given array list


The program starts by creating an ArrayList of String type named list_Book and adding ten elements to it using the add() method of ArrayList. The elements added to the list are "Python", "C", "Php", "Css", "MySQL", "C++", "Java", "HTML", "Bootstrap", and "Flask". Then, the program prints the elements of the list using the println() method of System.out object.

After that, the program sorts the elements of the list in alphabetical order using the sort() method of the Collections class. The sorted list is printed again using the println() method of System.out object.

This is because the sort() method of the Collections class is used to sort the elements of the list in alphabetical order. The sorted list is printed using the println() method of System.out object.

Source Code

import java.util.*;
class Sort_ArayList
{
	public static void main(String[] args)
	{
		List<String> list_Book = new ArrayList<String>();
		list_Book.add("Python");
		list_Book.add("C");    
		list_Book.add("Php");
		list_Book.add("Css");
		list_Book.add("MySQL");
		list_Book.add("C++");
		list_Book.add("Java");
		list_Book.add("HTML");
		list_Book.add("Bootstrap");
		list_Book.add("Flask");
		System.out.println("Before Sort : "+list_Book);
		Collections.sort(list_Book);
		System.out.println("After Sort : "+list_Book);    
	}
}

Output

Before Sort : [Python, C, Php, Css, MySQL, C++, Java, HTML, Bootstrap, Flask]
After Sort : [Bootstrap, C, C++, Css, Flask, HTML, Java, MySQL, Php, Python]

Example Programs