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
The best way to manipulate Options is using for
expressions.
for (a1 <- a; b1 <- b) yield a1.compare(b1) // Some(-1)
If at least one of the numbers is None
, the result is None
.
val x: Option[Int] = None
for (a1 <- a; b1 <- x) yield a1.compare(b1) // None
A compare function may be defined as
def compare(a: Option[Int], b: Option[Int]) = {
for (a1 <- a; b1 <- b) yield a1.compare(b1)
}.get
Updated:
If you want the Nones you can use pattern matching:
def compare(a: Option[Int], b: Option[Int]) = (a, b) match {
case (Some(a), Some(b)) => a.compare(b)
case (None, None) => 0
case (None, _) => -1 // None comes before
case (_, None) => 1
}
val list: List[Option[Int]] = List(List(Some(1), None, Some(4), Some(2), None))
val list2 = list.sortWith(compare(_, _) < 0)
// list2 = List(None, None, Some(1), Some(2), Some(4))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With