Collections.reverseOrder() in Java

Collections.reverseOrder(): This method is available in java.util.Collections class of Java.

Syntax:

<E> Comparator<E> java.util.Collections.reverseOrder()

This method returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

Parameters: NA

Returns: A comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

Exceptions: NA

Approach

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsreverseOrder {
    public static void main(String[] args) {

        List<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(12);
        arrlist.add(5);
        arrlist.add(78);
        arrlist.add(15);

        Collections.sort(arrlist, Collections.reverseOrder());

        System.out.println(arrlist);

    }
}

Output:

[78, 15, 12, 5]


No comments:

Post a Comment