# How can I sort options in a select box?

**URL:** <https://www.ruby-forum.com/t/how-can-i-sort-options-in-a-select-box/96116>\
**Category:** Rails\
**Created:** [June 26, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/how-can-i-sort-options-in-a-select-box/96116 "2006-06-26T15:12:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b38774/32.png) [@masonbrown](https://www.ruby-forum.com/u/masonbrown)\
**Post date:** [June 26, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/how-can-i-sort-options-in-a-select-box/96116/1 "2006-06-26T15:12:49Z")

</div>

Pretty much a newbie here, looking for help on select boxes in forms…

Is there any way in the view to sort the options of a drop-down select  
box?

My current code (within a larger \_form.rhtml file) is:

 Location \<%= select 'asset', 'location\_id', [["Select a Location", ""],["", ""]] + Location.find\_all.collect { |l| [l.name, l.id] } %\>

This starts the drop-down menu with a null option “Select a Location”  
followed by a blank line null option. Then it grabs all the locations  
out of a table linked like:

class Asset \< ActiveRecord::Base  
belongs\_to :location, :foreign\_key =\> “location\_id”  
end

Basically, it works, but it’s sorted by the id of the location. I’d  
like to sort it in ascending order by the name of the location.

Thanks!

-Mason

---

<div class="post-metadata">

**Author:** ![masonbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b38774/32.png) [@masonbrown](https://www.ruby-forum.com/u/masonbrown)\
**Post date:** [June 26, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/how-can-i-sort-options-in-a-select-box/96116/2 "2006-06-26T15:49:02Z")

</div>

\<%= select ‘asset’, ‘location\_id’, [[“Select a Location”, “”],[“”,  
“”]] + Location.find(:all, :order =\> ‘name’).collect { |l| [ l.name,  
l.id ] } %\>

If this is just part of a form, have a look at form\_for:

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000387)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

\<% form\_for :asset, @asset, :url =\> { :action =\> ‘edit’ } do |f| %\>  
\<%= f.select :location\_id, [[“Select a Location”, “”],[“”, “”]] +  
Location.find(:all, :order =\> ‘name’).collect { |l| [l.name, l.id]  
%\>  
\<% end %\>

-Jonathan.

---

<div class="post-metadata">

**Author:** ![masonbrown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b38774/32.png) [@masonbrown](https://www.ruby-forum.com/u/masonbrown)\
**Post date:** [June 27, 2006, 4:23am UTC](https://www.ruby-forum.com/t/how-can-i-sort-options-in-a-select-box/96116/3 "2006-06-27T04:23:46Z")

</div>

Jonathan V. wrote:

> \<%= select ‘asset’, ‘location\_id’, [[“Select a Location”, “”],[“”,  
> “”]] + Location.find(:all, :order =\> ‘name’).collect { |l| [ l.name,  
> l.id ] } %\>
> 
> If this is just part of a form, have a look at form\_for:  
> [ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000387)
> 
> \<% form\_for :asset, @asset, :url =\> { :action =\> ‘edit’ } do |f| %\>  
> \<%= f.select :location\_id, [[“Select a Location”, “”],[“”, “”]] +  
> Location.find(:all, :order =\> ‘name’).collect { |l| [l.name, l.id]  
> %\>  
> \<% end %\>
> 
> -Jonathan.

Thanks Jonathan. That’s exactly what I was looking for.

I didn’t know you could interrupt the Location.find\_all.collect thing  
with options to the find method. Learn something new every day.

-Mason
