# sorting hash ????

**URL:** <https://discuss.rubyonrails.org/t/sorting-hash/25839>\
**Category:** rubyonrails-talk\
**Created:** [March 5, 2008, 2:40am UTC](https://discuss.rubyonrails.org/t/sorting-hash/25839 "2008-03-05T02:40:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 5, 2008, 2:40am UTC](https://discuss.rubyonrails.org/t/sorting-hash/25839/1 "2008-03-05T02:40:09Z")

</div>

Hello RoR Developers,

I have tiny problem with sorting hash. I can not sort hash values.

i have hash which is created by yaml file. yaml file

template1: &nbsp;&nbsp;template\_of\_roster3: template\_of\_roster2 &nbsp;&nbsp;template\_of\_roster4: template\_of\_roster3

controller:

@get\_config=YAML.load(File.open("#{PATH\_IM}/.......yml")) &nbsp;&nbsp;&nbsp;&nbsp;@get\_config == Hash &nbsp;&nbsp;&nbsp;&nbsp;@enterprises==Hash @my\_hash=@get\_config["template1"]

and I use this hash in my selection like

view:

\<% &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;templateOptions = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@sites.each do |key,value| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;templateOptions = templateOptions + "\<option value=#{key}\>#{value}\</option\>" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%=select\_tag "templates", templateOptions,{'style' =\> 'width:100px' } %\>

in fact there is no problem here but when I update my hash like this @new\_hash={"new\_hash"=\>"new\_hash"} @my\_hash.update(@new\_hash)

it s sorted like, new\_hash template\_of\_roster2 template\_of\_roster3

but I want to see new\_hash at the last option. how can I do it? any idea.

thanks

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 5, 2008, 9:36am UTC](https://discuss.rubyonrails.org/t/sorting-hash/25839/2 "2008-03-05T09:36:37Z")

</div>

> Hello RoR Developers,
> 
> but I want to see new\_hash at the last option. how can I do it? any idea.

By not using a hash. A hash just isn't ordered in ruby (in 1.9 they  
are ordered by the order of insertion). ActiveSupport also provides an  
OrderedHash class.

Fred
