Hash.collect displaying in wrong order in view helper

I have this helper method in my application_helper, for some reason it
doesn’t output in the correct order:

def view_by_options(active)
html = “View The List by:  "
html +={ :popular => ‘Most Popular’, :new => “Newest”, :recommended
=>
“Recommended”}.collect do |key,val|
link_to_unless (key.to_s == active, val, params.merge(:viewBy =>
key))
end.join(”,")
html
end

resulting html always has ‘newest, most popular, then recommended’, not
‘most popular, newest, recommended’.

When I run similar code in the console it works fine…any ideas?

http://corelib.rubyonrails.org/classes/Hash.html

Hashes are unordered. If you want reliable ordering, you will need to
use an Array.

  • Gabriel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs