Java Collections.sort(List <T> list, Comparator <? super T> c)

Syntax

Collections.sort(List <T> list, Comparator <? super T> c) has the following syntax.

public static <T> void sort(List <T> list,  Comparator <? super T> c)

Example

In the following code shows how to use Collections.sort(List <T> list, Comparator <? super T> c) method.


/* w  w  w .  jav a  2  s  .c  om*/
import java.util.Collections;
import java.util.LinkedList;

public class Main {
   public static void main(String args[]) {  
      // create linked list object       
      LinkedList<Integer>  list = new LinkedList<Integer> ();  
      
      // populate the list 
      list.add(-2);  
      list.add(2);  
      list.add(-12);  
      list.add(8);  

      // sort the list
      Collections.sort(list, Collections.reverseOrder());  
      
      System.out.println("List sorted in natural order: ");      
      System.out.println(list);
        
   }
}

The code above generates the following result.