# Best way of sorting/grouping objects

**URL:** <https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204>\
**Category:** Rails\
**Created:** [April 2, 2010, 3:23am UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204 "2010-04-02T03:23:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 2, 2010, 3:23am UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204/1 "2010-04-02T03:23:32Z")

</div>

I have a Model called Page, which has a column called page\_type.  
I want to display all pages by page type (which can change/be added).

What is the best way of doing this? I was, in the view looping through  
each page\_type, then doing an SQL search for a Page with that  
page\_type. But I don’t want to call Models through the View…

I tried to create a laid out like this:

@hash[:type1] = {:object1\_id =\> object, :object2\_id =\> object }  
@hash[:type2] = {:object1\_id =\> object, :object2\_id =\> object }

etc.

Then using a partial to go through the types… but I’m getting errors.

Anyone have a good idea of how to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 2, 2010, 3:39am UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204/2 "2010-04-02T03:39:55Z")

</div>

Are you looking for something like this?

@pages = Page.all(:order =\> ‘page\_type’)

“order” is just a SQL ORDER fragment. Then display it in your view  
with:

= render :partial =\> ‘page’, :collection =\> @pages

I’m not sure if that’s too simplistic of an approach. Does that help?

Angelo

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 2, 2010, 7:32pm UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204/3 "2010-04-02T19:32:42Z")

</div>

That could work. But I need it to order by page\_order also. I would  
like to group them into 3 groups then sort them by that. In the end I  
want it to look something like this in html…

TYPE ONE  
Page 1  
Page 2

TYPE TWO  
Page 1  
Page 2

Before I was doing something like this (I had an array of each page  
type called page\_types):

\<% page\_types.each do |t| %\>  
\<%= t %\>'s  
\<% Page.find(:all, :conditions=\>{:page\_type=\>t}, :order=\>position  
%\>  
etc.  
\<% end %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 3, 2010, 2:52am UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204/4 "2010-04-03T02:52:21Z")

</div>

By any chance do you have a model named PageType? If you, you could  
set up a relationship between PageType and Page:

page.rb:  
belongs\_to :page\_type

page\_type.rb:  
has\_many :pages

Then display it with:

your\_controller.rb:  
@page\_types = PageType.all

your\_view.html.haml (or .erb, etc.)

- @page\_types.each do |page\_type|  
%h1= page\_type.name  
%ul  
= list\_of page\_type.pages do |page|  
= page.name

If you don’t already have a PageType model, or don’t want to introduce  
another model, you could probably use:

@page\_types = Page.all(:order =\> ‘page\_type’, :group =\> ‘page\_type’)

and then play around with the result of that. (At least I think that’s  
correct… haha, sorry if it’s not)

Good luck,  
Angelo

---

<div class="post-metadata">

**Author:** ![brent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ec9cab/32.png) [@brent](https://www.ruby-forum.com/u/brent)\
**Post date:** [April 8, 2010, 2:54am UTC](https://www.ruby-forum.com/t/best-way-of-sorting-grouping-objects/186204/5 "2010-04-08T02:54:01Z")

</div>

Thanks Angelo! That is a great idea. In the long run that would  
probably make more sense. But in the meantime I’ll look into  
the :group fix. What exactly does that do?
