Sort Integer in Descending Order in Java Example

java.uti.Collections Examples


In this example, we will take Integer wrapper class and sort it in descending order by using Collections.sort(names,Collections.reverseOrder()) method.

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

public class SortWrappersExample {

    public static void main(String[] args) {
        sortWrapperClassObjectsInDesc();
    }
    private static void sortWrapperClassObjectsInDesc() {
        List < Integer > names = new ArrayList < > ();
        names.add(100);
        names.add(20);
        names.add(10);
        names.add(50);

        System.out.println("Before Sorting : " + names);
        Collections.sort(names, Collections.reverseOrder());
        System.out.println("After Sorting : " + names);
    }
}

Output

Before Sorting : [100, 20, 10, 50]
After Sorting : [100, 50, 20, 10]

Reference



Comments