# Order Values in a Select Box Using select

**URL:** <https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054>\
**Category:** Rails\
**Created:** [October 16, 2008, 11:37am UTC](https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054 "2008-10-16T11:37:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [October 16, 2008, 11:37am UTC](https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054/1 "2008-10-16T11:37:27Z")

</div>

Been trawling for a while and know there must be an easy answer to  
this so reluctant to work round it.

My select box:

\<%= form.select( :data, { “Yes” =\> “1”, “No” =\> “0”},{:prompt =\> “–  
Select –”}) %\>

The problem is the data is ordered alphabetically so the drop down  
gives the options no, yes:

– Select – No Yes

instead of:

– Select – Yes No

Any thoughts?

Many thanks,

Mike

---

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [October 16, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054/2 "2008-10-16T12:49:11Z")

</div>

\<%= f.select( :data, { “Yes” =\> “1”, “No” =\> “0”}.sort.reverse,  
{:prompt =\> “–  
Select –”}) %\>

---

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [October 16, 2008, 12:58pm UTC](https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054/3 "2008-10-16T12:58:27Z")

</div>

mikej wrote:

> Been trawling for a while and know there must be an easy answer to  
> this so reluctant to work round it.
> 
> My select box:
> 
> \<%= form.select( :data, { “Yes” =\> “1”, “No” =\> “0”},{:prompt =\> “ï¿½  
> Select ï¿½”}) %\>
> 
> The problem is the data is ordered alphabetically so the drop down  
> gives the options no, yes:
> 
> ï¿½ Select ï¿½ No Yes
> 
> instead of:
> 
> ï¿½ Select ï¿½ Yes No
> 
> Any thoughts?
> 
> Many thanks,
> 
> Mike

Hashes don’t guarantee any particular order. Not sure if rails is  
sorting the hash by key as you suggest. Maybe try array of arrays.  
Ordinarily I’d expect the order to be maintained since its an array, but  
I’m not sure:

form.select :data , [[‘yes’,‘1’],[‘no’,‘0’]] , …

Regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![mikej](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mikej](https://www.ruby-forum.com/u/mikej)\
**Post date:** [October 16, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/order-values-in-a-select-box-using-select/151054/4 "2008-10-16T13:08:37Z")

</div>

Splendid,  
thanks.
