simple efficiency question: repeated iteration in the view

Let's say I get a list of all places from the database in the
controller:
  @places=Place.find(:all, :order=>"category_id")

And in the view I want to list them under category headings

Now let's imagine categories is a hash like this
{0=>'Museum',1=>'Gallery',2=>'Historic House'}

The only way I can think of doing it is by traversing the whole list
for every category, ie:

<% categories.each do | key,value | %>
<%= value %><br />
<% @places.each do | place | %>
<%= place.title if place.category_id==key %>
<% end %>
<% end %>

Is there a better way of doing this that doesn't involve traversing
the entire array so many times?

Thanks.

two ideas are:

1) execute 1 find for each category in the controller and pass the
results to the view as separate collections. Can't be sure of the
relative perfomance though.

2) use a CASE or if/else if block in the view to presort the places
into Collections on singlepass then render each collection under the
apporopriate category heading. This should be faster then traversing
the array multiple times but may clutter up you view logic

Brian

use #group_by. It takes your array of @places and creates a structure
like this:

{0 => [<place>, <place>], 1 => [<place>], ...}

@places=Place.find(:all, :order=>"category_id")
@category_places = @places.group_by { |p| p.category_id }
@categories = {0=>'Museum',1=>'Gallery',2=>'Historic House'}

@categories.each do |category_id, category|
  puts category
  @category_places[category_id].each do |place|
    puts place
  end
end

Rick - brilliant. Thanks.