Java Collections.sort(List <T> list)
Syntax
Collections.sort(List <T> list) has the following syntax.
public static <T extends Comparable <? super T>> void sort(List <T> list)
Example
In the following code shows how to use Collections.sort(List <T> list) method.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/*from w ww. j av a 2 s. co m*/
public class Main {
public static void main(String args[]) {
String init[] = { "One", "Two", "Three", "One", "from java2s.com", "Three" };
// create one list
List<String> list = new ArrayList<String>(Arrays.asList(init));
System.out.println("List value before: "+list);
// sort the list
Collections.sort(list);
System.out.println("List value after sort: "+list);
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »