no sort in a select

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

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.

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 Fornoles 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] ]