Last active
June 14, 2016 17:09
-
-
Save frgomes/99bbd36b39488cbd6237 to your computer and use it in GitHub Desktop.
Scala - Custom ordering
This file contains hidden or 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
case class Employee(id: Int, firstName: String, lastName: String) | |
object Employee { | |
// Note that because `Ordering[A]` is not contravariant, the declaration | |
// must be type-parametrized in the event that you want the implicit | |
// ordering to apply to subclasses of `Employee`. | |
implicit def orderingByName[A <: Employee]: Ordering[A] = | |
Ordering.by(e => (e.lastName, e.firstName)) | |
val orderingById: Ordering[Employee] = Ordering.by(e => e.id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pending review: