Skip to content

Instantly share code, notes, and snippets.

@milankinen
Last active December 26, 2015 00:09
Show Gist options
  • Save milankinen/7062280 to your computer and use it in GitHub Desktop.
Save milankinen/7062280 to your computer and use it in GitHub Desktop.
Scala ordering DSL example
object OrderingDSLExample extends App {
case class My(num: Int, str: String)
val myValues = List(My(3, "foo"), My(3, "bar"), My(5, "bar"), My(5, "foo"))
import OrderingDSL._
require(myValues.sortBy(m => (asc(m.num), asc(m.str))) ==
List(My(3, "bar"), My(3, "foo"), My(5, "bar"), My(5, "foo")))
require(myValues.sortBy(m => (asc(m.num), desc(m.str))) ==
List(My(3, "foo"), My(3, "bar"), My(5, "foo"), My(5, "bar")))
require(myValues.sortBy(m => (desc(m.num), asc(m.str))) ==
List(My(5, "bar"), My(5, "foo"), My(3, "bar"), My(3, "foo")))
require(myValues.sortBy(m => (desc(m.num), desc(m.str))) ==
List(My(5, "foo"), My(5, "bar"), My(3, "foo"), My(3, "bar")))
println("Horray!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment