# Sorting hash?

**URL:** <https://www.ruby-forum.com/t/sorting-hash/131004>\
**Category:** Rails\
**Created:** [March 5, 2008, 3:40am UTC](https://www.ruby-forum.com/t/sorting-hash/131004 "2008-03-05T03:40:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 3:40am UTC](https://www.ruby-forum.com/t/sorting-hash/131004/1 "2008-03-05T03: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:  
template\_of\_roster3: template\_of\_roster2  
template\_of\_roster4: template\_of\_roster3

controller:

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

and I use this hash in my selection like

view:

\<%  
templateOptions = “”  
@sites.each do |key,value|  
templateOptions = templateOptions + “#{value}”  
end  
%\>  
\<%=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:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [March 5, 2008, 10:37am UTC](https://www.ruby-forum.com/t/sorting-hash/131004/2 "2008-03-05T10:37:55Z")

</div>

On 5 Mar 2008, at 02:40, Ibrahim D. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/47e85d/32.png) [@casid](https://www.ruby-forum.com/u/casid)\
**Post date:** [October 14, 2008, 8:42am UTC](https://www.ruby-forum.com/t/sorting-hash/131004/3 "2008-10-14T08:42:23Z")

</div>

I think you have to do something like this

\<%  
templateOptions = “”  
@sites.sort {|key,value| value[1]\<=\>key[1]}.each do |  
v|  
templateOptions = templateOptions + “#{v[1]}”  
end  
%\>  
\<%=select\_tag “templates”, templateOptions,{‘style’ =\>  
‘width:100px’ } %\>

try it out

On Mar 5, 7:40 am, Ibrahim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
