David A. Black wrote: > Hi -- > > On Thu, 9 Jul 2009, Dipti Iyengar wrote: > >> Hi All, >> >> I ve to sort risks by "High" "Medium" "Low" "Info" and "Safe" >> >> When I sort in the model it sorts in the alphabetical order which is not >> what i want. >> >> Tried adding a position to each of it as High -> 1 Medium -> 2 etc. and >> then sorting by the position....but seems like too much of work >> >> Any quicker way to do this? > > You could do: > > LEVELS = %w{ High Medium Low Info Safe } > > sorted = risks.sort_by {|r| LEVELS.index(r.capitalize) } If there are a lot of levels, it might be more efficient to avoid the linear search and use a hash. It's not necesarily faster in this case with only 6 levels, but anyway... LEVELS = %w{ High Medium Low Info Safe } LEVEL_HASH = LEVELS.inject({}) {|h, lev| h[lev] = LEVELS.index(lev); h} risks = LEVELS + LEVELS sorted = risks.sort_by {|r| LEVEL_HASH[r.capitalize] } p sorted -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407