# Hash.collect displaying in wrong order in view helper

**URL:** <https://www.ruby-forum.com/t/hash-collect-displaying-in-wrong-order-in-view-helper/105422>\
**Category:** Rails\
**Created:** [May 29, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/hash-collect-displaying-in-wrong-order-in-view-helper/105422 "2007-05-29T21:08:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 29, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/hash-collect-displaying-in-wrong-order-in-view-helper/105422/1 "2007-05-29T21:08:29Z")

</div>

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:** &nbsp;&nbsp;"  
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?

---

<div class="post-metadata">

**Author:** ![blinking\_bear](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a9a28c/32.png) [@blinking\_bear](https://www.ruby-forum.com/u/blinking_bear)\
**Post date:** [May 29, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/hash-collect-displaying-in-wrong-order-in-view-helper/105422/2 "2007-05-29T21:20:32Z")

</div>

[http://corelib.rubyonrails.org/classes/Hash.html](http://corelib.rubyonrails.org/classes/Hash.html)

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

- Gabriel
