Java Collection Tutorial - Java Collections.sort(List <T> list)


Collections.sort(List <T> list) has the following syntax.

public static <T extends Comparable <? super T>> void sort(List <T> list)


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;
/*  www  . j  a  v  a2 s  .c o m*/
public class Main {
     public static void main(String args[]) {
      String init[] = { "One", "Two", "Three", "One", "from", "Three" };
      // create one list
      List<String> list = new ArrayList<String>(Arrays.asList(init));
      System.out.println("List value before: "+list);
      // sort the list
      System.out.println("List value after sort: "+list);

The code above generates the following result.