Collections.sort(List, Comparator): This method is available in java.util.Collections class of Java.
Syntax:
<E> void java.util.Collections.sort(List<E> list, Comparator<? super E> c)
This method takes two arguments. This method sorts the specified list according to the order induced by the specified comparator.
Parameters: Two parameters are required for this method.
list: the list to be sorted.
c: the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
Throws:
1. ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
2. UnsupportedOperationException - if the specified list'slist-iterator does not support the set operation.
3. IllegalArgumentException - if the comparator is found to violate the Comparator contract.
Approach
Java
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Collectionssort2 {public static void main(String[] args) {List<Integer> arrlist = new ArrayList<Integer>();arrlist.add(12);arrlist.add(56);arrlist.add(899);arrlist.add(65);arrlist.add(5);Collections.sort(arrlist, Collections.reverseOrder());System.out.println(arrlist);}}
Output:
[899, 65, 56, 12, 5]
No comments:
Post a Comment