Created
September 26, 2014 09:40
-
-
Save ponkotuy/6c8483211797219cf41b to your computer and use it in GitHub Desktop.
Scala Ordering Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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