Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created September 26, 2014 09:40
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 ponkotuy/6c8483211797219cf41b to your computer and use it in GitHub Desktop.
Save ponkotuy/6c8483211797219cf41b to your computer and use it in GitHub Desktop.
Scala Ordering Sample
object Main extends App {
assert(Ver(1, 4) > Ver(1, 2))
assert(Ver(2, 1) < Ver(3, 0))
assert(Ver(4, 0) == Ver(4, 0))
assert(List(Ver(1, 2), Ver(1, 4), Ver(1, 6)).min == Ver(1, 2))
}
case class Ver(major: Int, minor: Int) extends Ordered[Ver] {
def compare(other: Ver) = {
val major = this.major compare other.major
if(major == 0) { this.minor compare other.minor } else major
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment