by Dave Burt
"Full Documentation":http://www.dave.burt.id.au/ruby/sort_helper_doc/
"Download":http://www.dave.burt.id.au/ruby/sort_helper.rb

Click on the heading, and the table is sorted by that column.

The SortHelper will create links for your users to sort tables.

Here's a quick synopsis:

*View*
<pre>
  <tr>
    <th><%= link_to_sort_by 'First Name', 'name' %></th>
    <th><%= link_to_sort_by 'Surname', 'family.name' %></th>
    <th><%= link_to_sort_by 'Email', 'email' %></th>
  </tr>
</pre>

*Controller:*
<pre>
  helper :sort

  def list  # action
    SortHelper.columns = %w[
      name
      family.name
      email
    ]
    SortHelper.default_order = %w[family.name name]
    @people = Person.find(:all).sort do |a, b|
      SortHelper.sort(a, b, @params)
    end
  end
</pre>

And your @people are sorted!

----

Wouldn't this work better if the helper just set an @param[:order_by] and then use:

<pre>
@people = Person.find :all, :order => @param[:order_by]
</pre>

Good idea. This way sorting would work with paginators as well.
----

category:Helper
