How to sort by the boolean value?

I want to sort the people by the boolean value i.e., whose field will be true will come first then false one.
How I modified the following query

order_by: [desc_nulls_last: r.percent, asc: applicant.name, asc: applicant.boolean_field]

Your help will be appreciated. Is there any way or not?

I think you should use fragments with respect to this stackoverflow answer:

order_by: [
  desc_nulls_last: r.percent,
  asc: applicant.name,
  asc: fragment("case when ? then 1 when ? is null then 2 else 3 end", applicant.boolean_field, applicant.boolean_field)
]

desc? Assuming you have no nulls, since you didn’t mention what to do with them–normal order is false then true, that’s PG’s idea of “ascending” for this data type, so to reverse it just use desc:

1 Like