Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort database value without using ASC or DESC

I have an attribute in the database that I want to use a filter on by sorting it as ASC or DESC. I want the order of the elements to be

highest 
high
medium
low
lowest

Using the ASC $q->orderBy('priority', 'ASC'); the result given is

high
highest
low
lowest
medium

What can I do to sort the elements in that order? thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use a case statement in order by:

order by 
case column_name
when 'highest' then 5
when 'high' then 4
when 'medium' then 3
when 'low' then 2
when 'lowest' then 1
end desc

So something like this should work:

$q->orderByRaw("case column_name
    when 'highest' then 5
    when 'high' then 4
    when 'medium' then 3
    when 'low' then 2
    when 'lowest' then 1
    end desc");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading