Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Created January 25, 2011 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrechtel/795327 to your computer and use it in GitHub Desktop.
Save jbrechtel/795327 to your computer and use it in GitHub Desktop.
trait Ordered[A] {
def compare(that: A): Int
def < (that: A): Boolean = (this compare that) < 0
def > (that: A): Boolean = (this compare that) > 0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
}
}
class Person(val name: String) extends Ordered[Person] {
def compare (that: Person) = this.name.compare(that.name)
}
val tom = new Person("Tom")
val sue = new Person("Sue")
val thomas = new Person("Tom")
tom > sue //true
thomas < tom //false
tom >= thomas //true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment