Forum: Ruby on Rails Sorting an array of models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-13 02:58
I have a model, listing, which belongs_to one or more categories. I need
the user to be able to search for categories by name (which I can do)
and have it return all the listings that belong to those categories.
This I can do.

However, what I need to do is take that list of listings and sort them
by one of their fields. This is a bit more complicated, without needing
to sort I could just do:

<% for category in @categories %>
   <% for listing in category.listings %>
        <%= DISPLAY CODE %>
   <% end %>
<% end %>

But now I need to get an array of listings in my controller code, and
then sort that list. Is there an easy way to do that in Rails or do I
need to write my own sort code?

Thanks,

-Adam
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-13 03:15
(Received via mailing list)
On Apr 12, 2006, at 5:58 PM, Adam Bloom wrote:

>
> Thanks,
>
> -Adam
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


Look at Array#sort and Array#sort_by

-Ezra
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-04-13 03:18
(Received via mailing list)
Four ways to crack this nut.

1) retrieve the data from the database in the order you want it.  I
think if you add an :order=>'column' to the belongs_to :category
statement in your 'listings.rb' model file.  This should default to this
sort order

2) use the association function:  category.listings(:order=>'name')

3) use a find statement: Listings.find_by_category(category,
:order=>'name')

4) get the collection from the association function, and sort those

sorted_array = category.listings.sort {|a,b| a.name <=> b.name}

For the most part, I prefer to let the database do the sorting for me.

On Thursday, April 13, 2006, at 2:58 AM, Adam Bloom wrote:
>   <% for listing in category.listings %>
>-Adam
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


_Kevin
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-13 03:58
Thanks for the responses!

> Four ways to crack this nut.
>
> 1) retrieve the data from the database in the order you want it.  I
> think if you add an :order=>'column' to the belongs_to :category
> statement in your 'listings.rb' model file.  This should default to this
> sort order

This worked when I added the line to the line in categories.rb. I'm
guessing because it's actually a habtm. :)

> 2) use the association function:  category.listings(:order=>'name')
>
> 3) use a find statement: Listings.find_by_category(category,
> :order=>'name')
>
> 4) get the collection from the association function, and sort those
>
> sorted_array = category.listings.sort {|a,b| a.name <=> b.name}

The problem with these is that I have to sort listings from
(potentially) a bunch of different categories.

> For the most part, I prefer to let the database do the sorting for me.
>
> _Kevin

Thanks again,

Adam
This topic is locked and can not be replied to.