# Using hash.keys - why doesn't it display in order created

**URL:** <https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954>\
**Category:** Rails\
**Created:** [January 31, 2007, 1:46am UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954 "2007-01-31T01:46:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 1:46am UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/1 "2007-01-31T01:46:15Z")

</div>

I am using .keys on a hash to create an array that is than displayed  
in a view with .each - why does it not display the keys in the order  
added to the hash?

I would like to controll how the array is displayed - I want the  
checkboxes to display like I added them to the hash, but it doesn’t -  
I can’t figure out how it is displayed, when I change the order of the  
hash it does little to the view.

Please help - thanks, K

Code

## Controller

# creates my hash

def mod\_types  
{‘Learning Objects’ =\> LearnObjResource, ‘RSS Feed’ =\>  
RssResource, ‘Blank Module’ =\> BlankResource,‘Course Librarian’ =\>  
LibResource, ‘Professor Information’ =\> InstResource, ‘Course  
Assignment’ =\> AssignResource, ‘Style Guides’ =\> StyleResource,  
‘Plagiarism Information’ =\> PlagResource, ‘Instructor Recommends’ =\>  
RecomResource, ‘Course Reserves’ =\> ReserveResorce }  
end

#action for view  
def list\_modules  
@mods = @user.modules  
@list = mod\_types.keys # here I am making an array out of the hash  
if request.post?  
redirect\_to :action =\> “manage\_modules”  
end  
end

## View

…  
\<removed\_email\_address@domain.invalid do |mod|%\>  
\<%= check\_box\_tag(“mods[]”, mod ,false, {:class =\>  
“class”}) %\>  
\<%= mod%\>  
  
\<%end%\>

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 1:55am UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/2 "2007-01-31T01:55:16Z")

</div>

Hi~

On Jan 30, 2007, at 4:45 PM, Kim wrote:

> Please help - thanks, K  
> Assignment’ =\> AssignResource, ‘Style Guides’ =\> StyleResource,  
> end  
> end
> 
> ## View
> 
> …  
> \<removed\_email\_address@domain.invalid do |mod|%\>  
> \<%= check\_box\_tag(“mods[]”, mod ,false, {:class =\>  
> “class”}) %\>  
> \<%= mod%\>  
>   
> \<%end%\>

Hashes are unordered data structures. You can’t get them in order  
inserted with a normal hash. You will have to use an array or google  
for an ordered hash class for ruby.

Cheers-

– Ezra Z.  
– Lead Rails Evangelist  
– removed\_email\_address@domain.invalid  
– Engine Y., Serious Rails Hosting  
– (866) 518-YARD (9273)

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 1:57am UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/3 "2007-01-31T01:57:16Z")

</div>

Hi Kim,

Hashes are, by definition, unordered. Ruby does provide the sort method  
for  
Hash, though. See pg. 499 in Pickaxe for more info.

hth  
Bill

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/4 "2007-01-31T19:12:51Z")

</div>

I don’t want to sort the hash - I want to determine the order the  
values from the hash are displayed. I can’t use an array because of  
what I need to be able to do after users select values.

The view uses the keys form the hash - is there any way that I can  
controll the order they are displayed?

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/5 "2007-01-31T19:57:56Z")

</div>

as stated previously, Hash.sort, which you don’t want to do, so then  
the answer is no, that’s not to say you can’t roll your own solution.

off the top of my head…

def mod\_types  
[{ key1 =\> val1 }, {key2 =\> val2}, …]  
end

def list\_modules  
…  
@list = mod\_types.map { |mod\_type| mod\_type.keys.first }  
…  
end

your view shouldn’t have to change.

i would imagine it wouldn’t take much to modify whatever it is you are  
doing with users, but since you didn’t provide any details on that, I  
can’t provide any possible solutions.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/6 "2007-01-31T21:56:06Z")

</div>

Dictionary looks good except it does not have some of the methods that  
hash has that I need - thanks

I went with hash.keys.sort - not really what I want but at least it is  
in alphabetically order.

---

<div class="post-metadata">

**Author:** ![kim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/9f8e36/32.png) [@kim](https://www.ruby-forum.com/u/kim)\
**Post date:** [January 31, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/using-hash-keys-why-doesnt-it-display-in-order-created/83954/7 "2007-01-31T19:57:56Z")

</div>

Kim wrote:

> I don’t want to sort the hash - I want to determine the order the  
> values from the hash are displayed. I can’t use an array because of  
> what I need to be able to do after users select values.
> 
> The view uses the keys form the hash - is there any way that I can  
> controll the order they are displayed?

AFAIK, not out of the box, but there are ordered hash implementations  
available; check out the Dictionary class from Ruby F.s  
([http://facets.rubyforge.org/api/more/classes/Dictionary.html](http://facets.rubyforge.org/api/more/classes/Dictionary.html)).

Grab the code from

[http://facets.rubyforge.org/repo/lib/facets/more/dictionary.rb](http://facets.rubyforge.org/repo/lib/facets/more/dictionary.rb)

and you can use it in your app (drop it in ‘lib’ and require it as  
needed).
