Skip to content

Instantly share code, notes, and snippets.

@willf
Last active December 20, 2015 03:29
Show Gist options
  • Save willf/6063944 to your computer and use it in GitHub Desktop.
Save willf/6063944 to your computer and use it in GitHub Desktop.
arbitrary sort orderings using contramaps from scalaz (via Cody Allen)
import scalaz.Order
case class WeightedConcept(id: String, weight: Double)
val concepts = List(WeightedConcept("one", 1.0), WeightedConcept("two", 0.5))
val weightOrder: Order[WeightedConcept] = implicitly[Order[Double]].contramap[WeightedConcept](_.weight)
concepts.sorted(weightOrder.toScalaOrdering)
concepts.sorted(weightOrder.reverseOrder.toScalaOrdering)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment