# How to sort results of group\_by

**URL:** <https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074>\
**Category:** rubyonrails-talk\
**Created:** [December 5, 2008, 11:50pm UTC](https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074 "2008-12-05T23:50:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://discuss.rubyonrails.org/u/Patrick_Doyle)\
**Post date:** [December 5, 2008, 11:50pm UTC](https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074/1 "2008-12-05T23:50:17Z")

</div>

@areas.group\_by(&:responsibility).each do |responsibility, areas|

\<%=h [responsibility.name](http://responsibility.name) %\>

 \<% areas.each do |area| %\> ...

I see that #group\_by iterates through my list of areas, splitting them into sets according to which ones have the same responsibility.

I would like to sort the results of that list.

It seems to me that group\_by returns an OrderedHash. How to I sort those results prior to passing the |responsibility, areas| pairs?

–wpd

---

<div class="post-metadata">

**Author:** ![Ed14](https://avatars.discourse-cdn.com/v4/letter/e/c37758/32.png) [@Ed14](https://discuss.rubyonrails.org/u/Ed14)\
**Post date:** [December 6, 2008, 12:33am UTC](https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074/2 "2008-12-06T00:33:40Z")

</div>

The ordered hash is already sorted by responsibility, so you would sort each area something like this: (tags removed)

@areas.group\_by(&:responsibility).each do |responsibility, areas| &nbsp;&nbsp;responsibility.name &nbsp;&nbsp;areas.sort\_by{|area| area.name}.each do |area| &nbsp;&nbsp;...

A style suggestion: make yourself a method in class Area that returns @areas\_grouped\_and\_sorted. Less code in the view, and reusable.

---

<div class="post-metadata">

**Author:** ![Patrick\_Doyle](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@Patrick\_Doyle](https://discuss.rubyonrails.org/u/Patrick_Doyle)\
**Post date:** [December 6, 2008, 3:40am UTC](https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074/3 "2008-12-06T03:40:56Z")

</div>

The problem is that the responsibilities are not already sorted going in. Each area belongs\_to a responsibility. I have the list of all of the areas, I want to group them by responsibility, and I want to sort the result by some field within the responsibility. Currently, I have something like:

@areas = Area.find(:all)

Is there some way to order that ahead of time by areas.responsibility.some\_field?

Hmmm… perhaps I’m thinking of this backwards. (It wouldn’t be the first time.) Do I really want to do something like:

@responsibilities = Responsibility.find(:all)

Hmmmm… that looks like the right avenue to have taken in the first place…

now… if I could just figure out how to comment out blocks of ERB code, this would be much easier to test and then back out of if it goes awry.

–wpd

---

<div class="post-metadata">

**Author:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://discuss.rubyonrails.org/u/Steve_Ross)\
**Post date:** [December 7, 2008, 3:24am UTC](https://discuss.rubyonrails.org/t/how-to-sort-results-of-group-by/37074/4 "2008-12-07T03:24:15Z")

</div>

\<% if false -%\> commented out \<% end -%\>

Works for me when I don't have time to think up anything else...
