Java Collection Tutorial - 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;
/*  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 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.