Created
February 24, 2011 20:57
-
-
Save oluies/842870 to your computer and use it in GitHub Desktop.
-< Jason Zaugg
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 Person(last: String, first: String) | |
val ps = List(Person("Smith", "Bob"), Person("Smith", "Bill")) | |
val psS = ps.sortBy(p => (p.last, p.first)) | |
implicit val o = Ordering.by((p: Person) => (p.last, p.first)) | |
ps.sorted | |
case class Reverse[T](t: T) | |
implicit def ReverseOrdering[T: Ordering]: Ordering[Reverse[T]] = Ordering[T].reverse.on(_.t) | |
ps.sortBy(p => (p.last, Reverse(p.first))) | |
/** | |
* An answer from Jason Zaugg on the scala-language mailing list | |
* | |
* items.orderBy( item => item.foo ).thenBy ( item => item.bar ) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
scala> case class Reverse[T](t: T)
defined class Reverse
scala> implicit def ReverseOrdering[T: Ordering]: Ordering[Reverse[T]]
= Ordering[T].reverse.on(_.t)
ReverseOrdering: [T](implicit evidence$1: Ordering[T])Ordering[Reverse[T]]
scala> ps.sortBy(p => (p.last, Reverse(p.first)))
res21: List[Person] = List(Person(Smith,Bob), Person(Smith,Bill))