Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MyOrderingNum
case class MyOrderedNum(value: Int) extends Ordered[MyOrderedNum] {
override def compare(that: MyOrderedNum): Int = {
if (this.value < that.value) -1
else if (that.value < this.value) 1
else 0
}
}
case class MyOrderingNum(value: Int)(implicit ord: Ordering[MyOrderingNum]) {
def >(that: MyOrderingNum): Boolean = ord.compare(this, that) > 0
}
object MyNumExample extends App {
implicit object MyOrdering extends Ordering[MyOrderingNum] {
override def compare(x: MyOrderingNum, y: MyOrderingNum): Int = {
if (x.value < y.value) -1
else if (y.value < x.value) 1
else 0
}
}
assert(MyOrderedNum(10) > MyOrderedNum(2))
assert(MyOrderingNum(10) > MyOrderingNum(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment