Adding a custom sort

I have a field which is an atom. I want to sort it with a custom sort order: I am imagining something like this but I can;t find the exact syntax: prepare fn query, _ -> query |> Ash.Query.sort([ Ash.Sort.expr_sort( case :deliver_before do :"10am" -> 1 :"2pm" -> 2 :"6pm" -> 3 :"10pm" -> 4 :no_preference -> 5 end, :integer ) ]) end is this possible in ash?
2 Replies
ZachDaniel
ZachDaniel4mo ago
You can do it, you just need to use a cond statement since we can't do case statements in expressions I would also suggest to make this a calculation
calculate :deliver_before_sort, :integer, expr(
cond do
deliver_before == :"10am" -> 1
...
end
)
calculate :deliver_before_sort, :integer, expr(
cond do
deliver_before == :"10am" -> 1
...
end
)
Then you can Ash.Query.sort(query, :deliver_before_sort)
ajst7les
ajst7lesOP4mo ago
great thank you.

Did you find this page helpful?