Java examples for Collection Framework:Comparator
Using a Comparator object with method sort.
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); } }