Using a Comparator object with method sort. - Java Collection Framework

Java examples for Collection Framework:Comparator

Description

Using a Comparator object with method sort.

Demo Code

import java.util.List;
import java.util.Arrays;
import java.util.Collections;

public class Main 
{
   public static void main(String[] args)
   {/*from   w w w.  j a va 2 s . co  m*/
      String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

      // Create and display a list containing the suits array elements
      List<String> list = Arrays.asList(suits); // create List
      System.out.printf("Unsorted array elements: %s%n", list);

      // sort in descending order using a comparator
      Collections.sort(list, Collections.reverseOrder()); 
      System.out.printf("Sorted list elements: %s%n", list);
   } 
}

Result


Related Tutorials