Skip to content

Instantly share code, notes, and snippets.

@hiramekun
Created April 12, 2021 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiramekun/53fa3d8fcc353175a3e6c3b7864f0d7d to your computer and use it in GitHub Desktop.
Save hiramekun/53fa3d8fcc353175a3e6c3b7864f0d7d to your computer and use it in GitHub Desktop.
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