# Ordering column "name" in the List view

**URL:** <https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252>\
**Category:** Rails\
**Created:** [February 12, 2007, 12:03am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252 "2007-02-12T00:03:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [February 12, 2007, 12:03am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/1 "2007-02-12T00:03:57Z")

</div>

Hello,  
I am very new to RoR and am learning at a fairly good pace, but a  
seemingly simple task is leaving me stuck. I have a DB table called  
‘workers’ and fields for ‘first\_name’ and ‘last\_name’ need to be  
combined in the ‘list’ view and shown in order of last name. The code  
I have below will render the name as “Last, First”. How can I order  
the table by the name column when it is a combination of 2 fields?

–thanks

\<% for worker in @workers %\> \<% row\_color = cycle("F8F8F8", "ffffff")%\> \<% end %\>

| Name | Email | Phone Number #1 | Phone Number #2 | Options |
| --- | --- | --- | --- | --- |
| \<%= link\_to %Q{ #{worker.last\_name}, #{worker.first\_name}}, :action =\> 'show', :id =\> worker %\> | \<%= mail\_to worker.email, worker.email, :subject =\> "This is an example email", :body =\> "This is the body of the message." %\> | \<%= worker.telephone\_1 %\> | \<%= worker.telephone\_2 %\> | \<%= link\_to 'Edit', :action =\> 'edit', :id =\> worker %\> \<%= link\_to 'Delete', { :action =\> 'destroy', :id =\> worker }, :confirm =\> 'Are you sure?', :post =\> true %\> |

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [February 12, 2007, 12:44am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/2 "2007-02-12T00:44:01Z")

</div>

Hi,

I’m pretty new to RoR, too. But for what it’s worth, I think you need  
to look at your controller which populates your @workers collection  
and add an :order expression, something like:

find(:all, order=“name”)

If nothing else, look up “order” in a Rails book.

HTH,  
Richard

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [February 12, 2007, 1:20am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/3 "2007-02-12T01:20:38Z")

</div>

Richard wrote:

> Hi,
> 
> I’m pretty new to RoR, too. But for what it’s worth, I think you need  
> to look at your controller which populates your @workers collection  
> and add an :order expression, something like:
> 
> find(:all, order=“name”)
> 
> If nothing else, look up “order” in a Rails book.
> 
> HTH,  
> Richard

Right idea. You can pass an :order parameter to Model.find method that  
is basically an SQL fragment for setting the order of the results.

You should be fetching these workers in your controller, so change that  
line of code to something like:

@workers = Worker.find(:all, :order =\> ‘last\_name, first\_name’)

Which would generate SQL like:

SELECT \* FROM workers ORDER BY last\_name, first\_name

This way last\_name is the primary sort, and first\_name is the secondary  
sort in case of ties.

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [February 23, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/4 "2007-02-23T13:31:34Z")

</div>

Hi Alex,

On Feb. 11, you posted the reply “…Right idea. … @workers =  
Worker.find(:all, :order =\> ‘last\_name, first\_name’)” on the Google  
Groups’ thread  
[http://groups.google.com/group/rubyonrails-talk/browse\_frm/thread/cc00dc54ec438f28](http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/cc00dc54ec438f28).

You probably never noticed that I posted a follow-up question ten days  
later, which is repeated below with the hope that you might have an  
answer:

> @workers = Worker.find(:all, :order =\> ‘last\_name, first\_name’)

Nice job. But do you know where there’s an example of this where the  
app toggles between ASC and DESC when the column-name is clicked?

I received some suggestions about this a few months ago but never got  
it  
working right.

Do you have any idea where I can find an “applet” on the Web or in a  
book  
that demonstrates this.

Best wishes,  
Richard

On Feb 11, 7:20 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [March 4, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/5 "2007-03-04T23:37:59Z")

</div>

Try this. [sorttable: Make all your tables sortable](http://www.kryogenix.org/code/browser/sorttable/)

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [February 22, 2007, 12:20am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/6 "2007-02-22T00:20:41Z")

</div>

Hi Wayne,

> @workers = Worker.find(:all, :order =\> ‘last\_name, first\_name’)

Nice job. But do you know where there’s an example of this where the  
app toggles between ASC and DESC when the column-name is clicked?

I got some suggestions about this a few months ago but never got it  
working right. I’d like to find an “applet” on the Web or in a book  
that demonstrates this.

Regards,  
Richard

On Feb 11, 7:20 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [March 21, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/7 "2007-03-21T18:29:02Z")

</div>

I ended up using that too. It’s a great tool!

On Mar 15, 2:38 am, “Richard”

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [March 15, 2007, 7:49am UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/8 "2007-03-15T07:49:19Z")

</div>

Hi,

Thanks for that GREAT article. Of course, it’s all JavaScript rather  
than Ruby/Rails. But it works nicely and is designed beautifully, so  
it’ll be a great education for me to translate it to RoR with minimal  
JavaScript.

Best wishes,  
Richard

---

<div class="post-metadata">

**Author:** ![ebrad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/13edae/32.png) [@ebrad](https://www.ruby-forum.com/u/ebrad)\
**Post date:** [March 21, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/ordering-column-name-in-the-list-view/85252/9 "2007-03-21T18:30:03Z")

</div>

I forgot to thank you for this. It was helpful!

On Feb 11, 8:20 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
