Scala’s Ordering trait has a method reverse which seems to be the “official” way to get a TreeMap which is sorted “the wrong” way.
The snippet of the trait looks like this:
trait Ordering[T] extends Comparator[T] with PartialOrdering[T] with Serializable {
outer =>
override def reverse: Ordering[T] = new Ordering[T]{
override def reverse = outer
def compare(x: T, y: T) = outer.compare(y, x)
}
/*snip*/
}
I thought it would work comparable to Java’s Collections.reverseOrder, but Ordering.reverse doesn’t work of course.
How can I use the reverse Ordering with a TreeMap, e. g.:
new TreeMap[Foo, Bar](/*???*/)
Assuming, of course, that
Ordering[Foo]is implicitly available (such asOrdering[Int]orOrdering[String]). If you have it defined asobject X, just passX.reverseinstead.Note that the first set of parenthesis here must be empty — it is only the second set of parenthesis that receive the implicit parameter in this particular case.