Skip to content

Instantly share code, notes, and snippets.

@miguel-vila
Last active August 29, 2015 14:19
Show Gist options
  • Save miguel-vila/fbfc76661c25f9dc717e to your computer and use it in GitHub Desktop.
Save miguel-vila/fbfc76661c25f9dc717e to your computer and use it in GitHub Desktop.
import scalaz._
import Scalaz._
case class Cosa(str: String, f: Float, n: Int)
import scalaz.Order.orderBy
val orderByStr = orderBy { c: Cosa => c.str }
val orderByF = orderBy { c: Cosa => c.f }
val orderByN = orderBy { c: Cosa => c.n }
val combinedOrder = orderByStr |+| orderByF |+| orderByN
combinedOrder(Cosa("a",1.0f,2),Cosa("a",1.0f,2)) // EQ
combinedOrder(Cosa("a",1.0f,3),Cosa("a",1.0f,2)) // GT
combinedOrder(Cosa("a",1.0f,2),Cosa("a",1.0f,1)) // GT
combinedOrder(Cosa("a",1.0f,1),Cosa("a",1.0f,2)) // LT
combinedOrder(Cosa("ab",0.0f,1),Cosa("abc",0.0f,1)) // LT
implicit val cosaOrdering = combinedOrder.toScalaOrdering
val l = List(Cosa("ab",0.0f,1),Cosa("abc",0.0f,1),Cosa("defg",1.0f,2),Cosa("ab",1.0f,1),Cosa("abc",1.0f,2),Cosa("defg",1.0f,1))
l.sorted == List(Cosa("ab",0.0f,1), Cosa("ab",1.0f,1), Cosa("abc",0.0f,1), Cosa("abc",1.0f,2), Cosa("defg",1.0f,1), Cosa("defg",1.0f,2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment