# Beginner question about sort

**URL:** <https://www.ruby-forum.com/t/beginner-question-about-sort/73737>\
**Category:** Rails\
**Created:** [October 26, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/beginner-question-about-sort/73737 "2006-10-26T19:18:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_hutchinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sarah\_hutchinson](https://www.ruby-forum.com/u/sarah_hutchinson)\
**Post date:** [October 26, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/beginner-question-about-sort/73737/1 "2006-10-26T19:18:24Z")

</div>

Let’s say I have two models: location and employee

- employee belongs\_to locations
- locations has\_many employees

On a particular page of an app, I am looping through each location and  
then showing employees from that location.

In the controller, I am doing a search on all locations and listing  
them by location\_id.

In the view, I am looping through each location, and showing the  
employee details for that location. However, the employees being listed  
need to be shown in alphabetical order, but are being shown ordered by  
id.

Since I am not flat-out searching for the employees in the controller,  
how do I perform a sort on the employees to get them to list  
alphabetically?

Here is the code I am using to loop through the locations and  
employees:

 \<% @locations.each do |loc| %\> \<% loc.employees.in\_groups\_of(5) do |emp| %\> \<% emp.compact.each do |member| %\> \<% end %\> \<% end %\> \<% end %\>

| \<%= loc.name %\> |
| 
 \<%= link\_to(image\_tag( member.img\_sml, :alt =\> member.title, :class =\> "empimg", :size =\> "42x45" ), { :controller =\> "profile", :action =\> "show", :id =\> member.id } ) %\>  
 \<%= member.first\_name %\> \<%= member.last\_name %\>
 |

Thanks for your help,  
Sarah

---

<div class="post-metadata">

**Author:** ![sarah\_hutchinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@sarah\_hutchinson](https://www.ruby-forum.com/u/sarah_hutchinson)\
**Post date:** [October 26, 2006, 7:50pm UTC](https://www.ruby-forum.com/t/beginner-question-about-sort/73737/2 "2006-10-26T19:50:31Z")

</div>

sarah\_hutchinson wrote:

> employees:  
> \<%= link\_to(image\_tag( member.img\_sml, :alt =\> member.title, :class  
> =\> “empimg”, :size =\> “42x45” ), { :controller =\> “profile”,  
> :action =\> “show”,  
> :id =\> member.id } ) %\>  
>   
> \<%= member.first\_name %\> \<%= member.last\_name %\>  
> \<% end %\>
> 
> \<% end %\> \<% end %\>

Here’s one way:

\<% loc.employees.sort\_by {|x| x.last\_name +  
x.first\_name}.in\_groups\_of(5) do |emp| %\>

Or, in your Location model you can change do this:

has\_many :employees, :order =\> “last\_name, first\_name”

There are many other ways to do it. These are just a couple off the top  
of my head.

Jamey

Confidentiality Notice: This email message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and/or privileged information. If you are not the intended  
recipient(s), you are hereby notified that any dissemination,  
unauthorized review, use, disclosure or distribution of this email and  
any materials contained in any attachments is prohibited. If you receive  
this message in error, or are not the intended recipient(s), please  
immediately notify the sender by email and destroy all copies of the  
original message, including attachments.
