If I have two Options such as
val a = Option(2)
val b = Option(1)
I can write
List(a,b).sorted
and it sorts correctly by inserting an implicit Ordering. How can I get a reference to this Ordering so I can call compare(a,b)
and get the result? I'd like the equivalent of
val comparison = a.compare(b)
except without having a and b be instances of Ordered.
You can just ask for the ordering implicit directly:
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_21).
Type in expressions to have them evaluated.
Type :help for more information.
scala> implicitly[Ordering[Option[Int]]]
res0: Ordering[Option[Int]] = scala.math.Ordering$$anon$3@501a9177
scala> res0.compare(Some(1), Some(3))
res1: Int = -1