# Table sorting

**URL:** <https://www.ruby-forum.com/t/table-sorting/93956>\
**Category:** Rails\
**Created:** [April 16, 2006, 5:28am UTC](https://www.ruby-forum.com/t/table-sorting/93956 "2006-04-16T05:28:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 5:28am UTC](https://www.ruby-forum.com/t/table-sorting/93956/1 "2006-04-16T05:28:45Z")

</div>

Ahoy,

I checked the API and did some forum searches but didn’t find anything  
good on table sorting.

Is there a “railish” way to do this? My current method really is lacking  
(i’m on like rails day 2)

```
<th><%= link_to 'Name', :action => 'list', :order => 'name' %></th>
<th><%= link_to 'Note', :action => 'list', :order => 'note' %></th>
<th><%= link_to 'Created on', :action => 'list', :order => 

```

‘created\_on’ %\>

\<%= link\_to ‘Category’, :action =\> ‘list’, :order =\>  
‘category\_id’ %\>

This doesn’t allow switching between ASC and DESC either. plus I feel  
that it could be automagic to some degree?

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 5:30am UTC](https://www.ruby-forum.com/t/table-sorting/93956/2 "2006-04-16T05:30:37Z")

</div>

Oh, forgot this part:  
def list  
@item\_pages, @items = paginate(:items,  
:per\_page =\> 10,  
:order\_by =\> (params[:order]))  
end

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 7:08am UTC](https://www.ruby-forum.com/t/table-sorting/93956/3 "2006-04-16T07:08:26Z")

</div>

Will Jessup wrote:

> Oh, forgot this part:  
> def list  
> @item\_pages, @items = paginate(:items,  
> :per\_page =\> 10,  
> :order\_by =\> (params[:order]))  
> end

Does it hold the order when you press ‘next page’? Problem I’ve got is  
that I can order the table page that is displayed, but as soon as I  
press ‘next page’ the order is not sorted. I am looking for the ‘rails  
way’ to do this.

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 7:21am UTC](https://www.ruby-forum.com/t/table-sorting/93956/4 "2006-04-16T07:21:49Z")

</div>

Jeff Gordon wrote:

> Will Jessup wrote:
> 
> > Oh, forgot this part:  
> > def list  
> > @item\_pages, @items = paginate(:items,  
> > :per\_page =\> 10,  
> > :order\_by =\> (params[:order]))  
> > end
> 
> Does it hold the order when you press ‘next page’? Problem I’ve got is  
> that I can order the table page that is displayed, but as soon as I  
> press ‘next page’ the order is not sorted. I am looking for the ‘rails  
> way’ to do this.

I havn’t even gotten that far =]

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 11:37am UTC](https://www.ruby-forum.com/t/table-sorting/93956/5 "2006-04-16T11:37:26Z")

</div>

Ok this is just my rails day 3 but in my PHP pages I stored the sort  
order in the session for every table… You would have something like

session[:your\_table\_order] = params[:order] || your\_default\_order

and in paginate, you’ll have to :order\_by =\> session[:your\_table\_order]

You get the order from a GET or POST parameter, the table will only be  
sorted if this parameter is present. If you look at the links for “next  
page” and “previous page” you will notice that they don’t send this  
order parameter --\> The next page won’t be ordered. I’m quite sure there  
is a way to do this without sessions…

Please correct me, if I’m wrong.

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/table-sorting/93956/6 "2006-04-16T14:07:16Z")

</div>

Is this secure, or should params[:order] be validated against acceptable  
values first?

Will Jessup wrote:

> Oh, forgot this part:  
> def list  
> @item\_pages, @items = paginate(:items,  
> :per\_page =\> 10,  
> :order\_by =\> (params[:order]))  
> end

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/table-sorting/93956/7 "2006-04-16T15:24:50Z")

</div>

I dont see why it would be insecure. If someone happened to type a non  
existing column name in there they would (should) get an error back. I  
don’t  
think there is much you can do to screw up the process in the order  
clause.  
And this is also the way I do it as well.

I also preform one more check on the parameters to see it is already in  
the  
session, if it is I switch it between ASC or DESC depending on which is  
already set. My code for this is a little messy right now, but I could  
most  
certainly go back and clean it up if I’m ever that area again.

-Nick

---

<div class="post-metadata">

**Author:** ![wjessup](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wjessup](https://www.ruby-forum.com/u/wjessup)\
**Post date:** [April 16, 2006, 9:57pm UTC](https://www.ruby-forum.com/t/table-sorting/93956/8 "2006-04-16T21:57:53Z")

</div>

There is a SortHelper that you may use as a starting point  
[http://wiki.rubyonrails.com/rails/pages/Sort+Helper](http://wiki.rubyonrails.com/rails/pages/Sort+Helper)
