+ Start a Discussion
TheMythicalTheMythical 

Can I sort the data by the value in field priority?

I want to sort the data by the value in field priority. There are three values in that picklist field: High, Medium, Low.

 

If I only fire the query select XXX from Case order by priority. The result I get back will be High->Low->Medium (by letter order). But what I want is High->Medium->Low (based on the meanings). Are there any way I can write the SOQL to achieve this? Thanks!

 

Sincerely

TheMythical

wesnoltewesnolte

Hey

 

Unfortunately not. You'd have to either select all the data out and then loop through the list of records sorting them and putting them into an array/list or you select all records with each type of priority into their own lists. Depending on what you need to do you could use option 2 to create a list of lists.

 

Cheers,

Wes 

SuperfellSuperfell
a 3rd option would be to add a formula field that maps the priority text to a numerical value, and then run your query ordering by the numerical value instead.
wesnoltewesnolte

I think what he means by third option is best option :D Never thought of that, exellent suggestion:)

 

Wes

TheMythicalTheMythical
Could you please provide more details how I to map in formula? Thanks!
SuperfellSuperfell

I created a new forumula field of type Number, and used this formula

 

CASE( priority__c , "High", 1, "Medium", 2, "Low", 3, 4)

 

 

I called the field priorityInt, so then in SOQL you'd do

select name, priority__c from contact order by priorityInt__c 

 

and the rows would appear in order high, medium, low, unset.