« Return to Thread: Help required on sorting

Re: Help required on sorting

by David A. Black :: Rate this Message:

Reply to Author | View in Thread

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) }


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)

 « Return to Thread: Help required on sorting