2

Consider the following list with Pojo Object:

List<Pojo> list = new ArrayList<>();
....
class Pojo {
    private int field1;
    private int field2;
    ...
}

Note: Pojo can have more than two fields

I'm sorting my list of Pojo-s by ascending order:

Collections.sort(list, new Comparator<Pojo>() {

    @Override
    public int compare(Pojo o1, Pojo o2) {
        int fieldCompareTo = compare(o1.field1, o2.field1);

            if (fieldCompareTo == 0) {
                fieldCompareTo = compare(o1.field2, o2.field2);

            }
            return fieldCompareTo;
        }
    });

private static int compare(int a, int b) {
    return a < b ? -1
         : a > b ? 1
         : 0;
}

Here I'll get list sorted by ascending order.

My Questions is: If I reverse the list, will I get descending order?

I'm just calling Collections.reverse(list)

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Say, a sorted list is `[1, 2, 3, 4]`, and reversing it gives `[4, 3, 2, 1]`. Is this the scenario you speak of? – cs95 Jul 12 '17 at 22:08
  • Yes. But I'm sorting Class objects, not primitives. –  Jul 12 '17 at 22:08
  • It doesn't matter what you compare, since you told Java how to compare your objects. And `Collections.reverse(list)` just reverts the list, without further comparisons. – Tom Jul 12 '17 at 22:09
  • *FYI:* Your `compare(int a, int b)` method is already implement by the Java 7+ Runtime Library: [`Integer.compare(int a, int b)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#compare-int-int-) – Andreas Jul 12 '17 at 22:24

2 Answers2

10

Yes, reversing a list, that is sorted in ascending order, will give you list that is sorted in descending order.

However, it may not give you the same result as sorting a list in descending order.

That is because Collections.sort() is guaranteed to be stable, i.e. equal elements will not be reordered as a result of the sort.

If your list has multiple objects, that according to Comparator are "equal", but are not fully the same (i.e. can be distinguished), reversing the list will reverse their order, but sorting descending will leave them in order.

Example: Say you have a list of names: John Doe, Jane Smith, Jim Doe

If you sort that by last name only, John Doe and Jim Doe are equal, and will stay in that order.

So, sort ascending gives you: John Doe, Jim Doe, Jane Smith
Reversing that gives you: Jane Smith, Jim Doe, John Doe

That is a descending order by last name, however it is not the same as sorting the list in descending order.

A stable descending sort gives you: Jane Smith, John Doe, Jim Doe

Of course, if there are no two elements that are equal according to the Comparator, reversing an ascendingly sorted list is exactly the same as sorting the list descending.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thanks @Andreas. Though I think I'll not get the issue you mentioned, If I sort both last name and first name (in ascending order). Right? –  Jul 12 '17 at 22:48
  • @JoseDaniels Unless you sort by full name, but have two `Joe Smith` of different age, then you would still be able to detect a difference, by the order of age. It may not matter to you. This answer was just to ensure that you knew that Ascending + Reverse is not always the same as Descending, unless order is *absolute*, i.e. no two distinct objects are ordered equal. If your objects come from a database, they will usually all have unique IDs, so a final compare by ID can be used to ensure absolute ordering, if that is important. I often do that in SQL queries, to ensure *consistent* ordering. – Andreas Jul 13 '17 at 00:27
  • Agree. I always append the `_id` to the sorting fields. –  Jul 13 '17 at 00:49
  • 2
    @JoseDaniels: See https://stackoverflow.com/a/8312128/56778 for a simple explanation of the difference between stable and unstable sorts. – Jim Mischel Jul 13 '17 at 02:37
2

Calling Collections.reverse(list) will reverse the list as it exists in its current state. For example, if your list is [4,1,3,2], and you call reverse(), it will become [2,3,1,4]. It does not do a reverse of their natural ordering (unless the list is already in its natural order).

In order to have your list in reverse natural order, you would need to do something like this:

Collections.sort(list);
Collections.reverse(list);
Todd
  • 30,472
  • 11
  • 81
  • 89