3

Since Comparator<T> is a functional interface, I can do this

Comparator<Instant> instantComparator = Instant::compareTo;

And I can now reverse it:

Comparator<Instant> instantComparatorReversed = instantComparator.reversed();

I can do this directly, however:

Comparator<Instant> cannotDoThis = (Instant::compareTo).reversed();

which makes sense since Instant::compareTo after all is just a method, and in that context it will be just that. The only say to do it inline seems to be this:

Comparator<Instant> instantComparatorReversedWithCast = ((Comparator<Instant>)Instant::compareTo).reversed();

There is also

Comparator.<Instant>reverseOrder()

and as mentioned in comments

Comparator.<Instant>naturalOrder().reversed()

The question now is: is there a shorter/prettier way to do it "inline" than that?

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93

5 Answers5

4

You can write a method that accepts a Comparator as argument and reverses it:

public static <T> Comparator<T> reverse(Comparator<T> comparator) {
    return comparator.reversed();
}

You would probably do this in a class full of utility methods and statically import the method.

Then you can do:

reverse(Instant:compareTo)
Sweeper
  • 213,210
  • 22
  • 193
  • 313
3

Here's a shorter but uglier version:

Comparator<Instant> c = (a, b) -> b.compareTo(a);

with method references the best you can do is the first one (without getting into really ugly casts), so you'll have to decide whether you want short or readable.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
3

If Instant implements Comparable, you can do simply:

Comparator<Instant> c = Comparator.<Instant>naturalOrder().reversed();
lexicore
  • 42,748
  • 17
  • 132
  • 221
1

Since you are trying to just reverse a natural ordering, you can simply do:

Comparator<Instant> c = Comparator.reverseOrder();

If this was not a natural ordering, you could reverse it with reverseOrder from the Collections class (although the fact that those two static methods from different standard classes are named the same can be confusing):

Comparator<Instant> c = Collections.reverseOrder(Instant::compareTo);

If you want to reverse a Comparator made from a key extractor method reference, see this question: Comparator.reversed() does not compile using lambda

Misha
  • 27,433
  • 6
  • 62
  • 78
0

Using Comparator.reverseOrder() works to fulfil the main purpose (sorting it in reverse):

List<Instant> rev = ImmutableList.of(Instant.now(), Instant.now().plusSeconds(2), Instant.now().minusSeconds(5)).stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93