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))


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?


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

- Gabriel