1, "2 - ausreichend" => 2, "3 - verbesse...">

How to sort options_of_select hash?

I got something like this:

options_for_select({ "1 - optimal" => 1, "2 - ausreichend" => 2, "3 - verbesserungsfรคhig" => 3, "4 - nicht ausreichend" => 4, "5 - gar nicht" => 5})

Rails does not sort these entries in the output. How can I get a select box that needs to be sorted numerically?

+5
source share
1 answer

I believe in Ruby1.9, this will work the way you intend (hashes keep the order in which they are inserted), so if you use 1.9, this is the option you made.

Otherwise, you can use an array instead of a hash:

options_for_select([["1 - optimal", 1], ["2 - ausreichend", 2], ..., ["5 - gar nicht", 5]])
+6
source

All Articles