# Help required on sorting

**URL:** <https://www.ruby-forum.com/t/help-required-on-sorting/171792>\
**Category:** Ruby\
**Created:** [July 9, 2009, 11:46am UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792 "2009-07-09T11:46:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 11:46am UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/1 "2009-07-09T11:46:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 12:04pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/2 "2009-07-09T12:04:20Z")

</div>

Hi,

Am Donnerstag, 09. Jul 2009, 18:46:40 +0900 schrieb Dipti Iyengar:

> 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.

ary.sort\_by { |elem|  
case elem.risk  
when /high/i then 5  
when /medium/i then 4  
when /low/i then 3  
when /info/i then 2  
when /safe/i then 1  
else 0  
end  
}

Bertram

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/3 "2009-07-09T12:12:31Z")

</div>

Hi –

On Thu, 9 Jul 2009, Dipti Iyengar wrote:

> 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

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 1:05pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/4 "2009-07-09T13:05:22Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Thu, 9 Jul 2009, Dipti Iyengar wrote:
> 
> > 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

Thanks a lot Bertram … Worked like a charm…  
Thanks a lot David…didn’t try the solution as yet 🙂

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/5 "2009-07-09T13:48:20Z")

</div>

Hi,

Am Donnerstag, 09. Jul 2009, 19:11:46 +0900 schrieb David A. Black:

> On Thu, 9 Jul 2009, Dipti Iyengar wrote:
> 
> > 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.
> 
> LEVELS = %w{ High Medium Low Info Safe }  
> sorted = risks.sort\_by {|r| LEVELS.index(r.capitalize) }

Gosh, this is cool! I’m ashamed of my poor case approach.

Bertram

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/6 "2009-07-09T19:06:02Z")

</div>

David A. Black wrote:

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

---

<div class="post-metadata">

**Author:** ![diptisampath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4491bb/32.png) [@diptisampath](https://www.ruby-forum.com/u/diptisampath)\
**Post date:** [July 9, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/help-required-on-sorting/171792/7 "2009-07-09T13:51:51Z")

</div>

Hi –

On Thu, 9 Jul 2009, Bertram S. wrote:

> > sorted = risks.sort\_by {|r| LEVELS.index(r.capitalize) }
> 
> Gosh, this is cool! I’m ashamed of my poor case approach.

It comes from years of Ruby training where I use a deck of cards as  
one of the main examples:

RANKS = %w{ 2 3 4 5 6 7 8 9 10 j q k a }

def \<=\>(other)  
RANKS.index(rank) \<=\> RANKS.index(other.rank)  
end

🙂

David
