Friday, August 28, 2009

Sorting elements of a Collection in Java

Java Collections class provided method called sort which is used to sort List.
Ex.Collections.sort(list):

Java Program to sort elements using Collections.sort:
String names = "Ali Anuj Munjal Jigar Kamesh Harish Kamesh";
List lstoriginal = Arrays.asList(names.split(" "));
System.out.println("Element in List"+lstoriginal);

Collections.sort(lstoriginal);
System.out.println("Arrays After sorting : "+lstoriginal);


Output :
Element in List[Ali, Anuj, Munjal, Jigar, Kamesh, Harish, Kamesh]
Arrays After sorting : [Ali, Anuj, Harish, Jigar, Kamesh, Kamesh, Munjal]

Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/