Collections contains the sort(List<T>) method, which is used to sort the specified list into ascending order, according to the natural ordering of its element.
Declaration :
declaration of method - java.util.Collections.sort() method.
public static <T extends Comparable<? super T>> void sort(List<T> list)
Parameters :
list - Ihis is the list to be sorted.
Example :
******************************************************************************
package org.javaIsEasy.collectionPrograms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class SortListExample {
public static void main(String[] args) {
ArrayList<String> programmingLanguageList=new ArrayList<String>();
programmingLanguageList.add("PHP");
programmingLanguageList.add(".NET");
programmingLanguageList.add("C++");
programmingLanguageList.add("JAVA");
programmingLanguageList.add("PERL");
System.out.println("Display List Before Sorting\n");
Iterator<String> it=programmingLanguageList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
Collections.sort(programmingLanguageList);
System.out.println("Display List After Sorting\n");
Iterator<String> itSwapped=programmingLanguageList.iterator();
while(itSwapped.hasNext())
{
System.out.println(itSwapped.next());
}
}
}
******************************************************************************
OUTPUT :
******************************************************************************
Display List Before Sorting
PHP
.NET
C++
JAVA
PERL
Display List After Sorting
.NET
C++
JAVA
PERL
PHP
******************************************************************************
No comments:
Post a Comment