# No sort in a select

**URL:** <https://www.ruby-forum.com/t/no-sort-in-a-select/148535>\
**Category:** Rails\
**Created:** [September 14, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535 "2008-09-14T14:38:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [September 14, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535/1 "2008-09-14T14:38:00Z")

</div>

hi,  
i’m trying to add a select box with the select method, but rails orders  
it according to the name, while i needed ordered according to the value,  
or just to work in the order i put it in.  
this is my array:  
@price\_range = {"$100-$200" =\> “0”, “$200-$300” =\> “1”, “$300-$350” =\>  
“2”, “$350-$500” =\> “3”}  
and this is my view:  
\<% fields\_for(@description) do |d| -%\>  
\<%= d.select( :price\_range, @price\_range ) %\>  
\<%end$\>  
i can’t figure out how to do this, it subtracts one number from the  
other number (100 from 200) and then orders from lowest to highest,  
which is not what I want.  
any help is greatly appreciated,  
thanks

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [September 14, 2008, 2:56pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535/2 "2008-09-14T14:56:44Z")

</div>

On Sep 14, 8:38 pm, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## \<%end$\> i can’t figure out how to do this, it subtracts one number from the other number (100 from 200) and then orders from lowest to highest, which is not what I want. any help is greatly appreciated, thanks
> 
> Posted viahttp://www.ruby-forum.com/.

That’s no array. It’s a hash, and hashes are unordered - or at the  
very least you can never rely on its ordering.

Try this instead:

\<%= d.select(:price\_range, @price\_range.sort) %\>

Hope that works.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [September 14, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535/3 "2008-09-14T15:10:11Z")

</div>

thanks,  
that works great.  
is there a way to do this without having to sort? just having them in  
the order i placed them?  
Erol F. wrote:

> On Sep 14, 8:38ï¿½pm, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ## \<%end$\> i can’t figure out how to do this, it subtracts one number from the other number (100 from 200) and then orders from lowest to highest, which is not what I want. any help is greatly appreciated, thanks
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> That’s no array. It’s a hash, and hashes are unordered - or at the  
> very least you can never rely on its ordering.
> 
> Try this instead:
> 
> \<%= d.select(:price\_range, @price\_range.sort) %\>
> 
> Hope that works.

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [September 14, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535/4 "2008-09-14T15:16:39Z")

</div>

On 14 Sep 2008, at 14:10, Daniel F. wrote:

> thanks,  
> that works great.  
> is there a way to do this without having to sort? just having them in  
> the order i placed them?  
> Use an array instead of a hash (which is what sort does - turns the  
> hash into an array of [key, value] arrays.

Fred

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [September 14, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/no-sort-in-a-select/148535/5 "2008-09-14T15:36:34Z")

</div>

On Sep 14, 9:15 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 14 Sep 2008, at 14:10, Daniel F. wrote:
> 
> > thanks,  
> > that works great.  
> > is there a way to do this without having to sort? just having them in  
> > the order i placed them?
> 
> Use an array instead of a hash (which is what sort does - turns the  
> hash into an array of [key, value] arrays.
> 
> Fred

Which means you’ll need to change your @price\_range declaration to an  
array of pairs:

@price\_range = [[“$100-$200”, 0], [“$200-$300”, 1], [“$300-$350”, 2],  
[“$350-$500”, 3] ]
