Java Programming Tutorials

Java programming tutorials with many code examples!

Java reverse Comparator

Problem:

How to sort Java array/List in reverse order? It can be done with Reverse Comparator that can be created using Java Collections API. This example demonstrates its usage with Java Arrays and Lists.

Solution:

The following example shows how to use Collections.reverseOrder() method from java.util package. The method creates a Comparator, which imposes ordering reverse to natural ordering. Which means that it is applicable for objects implementing Comparable interface (that includes numbers, strings, etc.):

package com.farenda.java;

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

import static java.util.Arrays.asList;
import static java.util.Arrays.copyOf;

public class CollectionsReverseOrder {

    // Orders numbers in natural order
    static class NumberComparator implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1.compareTo(o2);
        }
    }

    public static void main(String[] args) {
        Integer[] scores = {77, 43, 84, 101};

        Integer[] array = copyOf(scores, scores.length);
        onArray(array);

        List<Integer> list = asList(copyOf(scores, scores.length));
        onList(list);

        list = asList(scores);
        withComparator(list);
    }

    private static void onArray(Integer[] scores) {
        System.out.println("Scores array: " + Arrays.toString(scores));

        Arrays.sort(scores);
        System.out.println("Sorted: " + Arrays.toString(scores));

        Arrays.sort(scores, Collections.reverseOrder());
        System.out.println("Reversed: " + Arrays.toString(scores));
    }

    private static void onList(List<Integer> scores) {
        System.out.println();
        System.out.println("Scores List: " + scores);

        Collections.sort(scores);
        System.out.println("Sorted: " + scores);

        Collections.sort(scores, Collections.reverseOrder());
        System.out.println("Reversed: " + scores);
    }

    private static void withComparator(List<Integer> scores) {
        Comparator<Integer> cmp = new NumberComparator();

        System.out.println();
        System.out.println("Scores List: " + scores);

        Collections.sort(scores, cmp);
        System.out.println("Sorted (comparator): " + scores);

        Collections.sort(scores, Collections.reverseOrder(cmp));
        System.out.println("Reversed (comparator): " + scores);
    }
}

The last method is using Collections.reverseOrder(Comparator) to create reverse comparator, which can be used to compare in reverse order objects that don’t implement Comparable interface.

The example is a bit simplified, because we created a Comparator for numbers, which are already comparable, but you get the point. :-)

Running the Java code gives the following result:

Scores array: [77, 43, 84, 101]
Sorted: [43, 77, 84, 101]
Reversed: [101, 84, 77, 43]

Scores List: [77, 43, 84, 101]
Sorted: [43, 77, 84, 101]
Reversed: [101, 84, 77, 43]

Scores List: [77, 43, 84, 101]
Sorted (comparator): [43, 77, 84, 101]
Reversed (comparator): [101, 84, 77, 43]

For another use of Comparator see Java Collection Min/Max example.

Share with the World!