Skip to content

Instantly share code, notes, and snippets.

@ptrdom
Created March 26, 2019 13:32
Show Gist options
  • Save ptrdom/b809e81c8b6adf54d2afd0066d2c7ec4 to your computer and use it in GitHub Desktop.
Save ptrdom/b809e81c8b6adf54d2afd0066d2c7ec4 to your computer and use it in GitHub Desktop.
Scala Slick code that does sorting by Enum while assigning different values to be used for sorting
import scala.language.implicitConversions
object OrderEnum extends Enumeration {
protected case class Val(actualValue: String, orderValue: String) extends super.Val
implicit def valueToVal(x: Value): Val = x.asInstanceOf[Val]
val A = Val("a", "c")
val B = Val("b", "b")
val C = Val("c", "a")
}
val q = persons
.sortBy { i =>
OrderEnum.values.drop(1)
.foldLeft(
Case.If(i.name === OrderEnum.values.head.actualValue)
.Then(Some(OrderEnum.values.head.orderValue): Rep[Option[String]])
) {
case (acc, enum) =>
acc.If(i.name === enum.actualValue).Then(Some(enum.orderValue): Rep[Option[String]])
}
.Else(Option.empty[String]: Rep[Option[String]])
.asc
}
db.run(q.result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment