Complex order_by

I’m trying to achieve this:

from(u in User, order_by: [case u.state do "available" -> 1; "busy" -> 2; _ -> 3 end])

Is there a way to do that?

1 Like

Figured out:

from(u in User, 
  order_by: fragment("CASE u0.state WHEN ? THEN 1 WHEN ? THEN 2 ELSE 3 END", "available", "busy")
)
|> Repo.all
2 Likes