# Organizing a Hash Map

**URL:** <https://www.ruby-forum.com/t/organizing-a-hash-map/137412>\
**Category:** Rails\
**Created:** [May 12, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412 "2008-05-12T22:46:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 12, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412/1 "2008-05-12T22:46:46Z")

</div>

Hello,  
I have a hash map that gets pumped full of info. The key is a  
username… I’m using a @myhashmap.each do |key, item| to display the  
hash map in my rhtml, but I need it to be alphabetized… Is there a  
built-in way to do this?

Thanks,

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 12, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412/2 "2008-05-12T22:53:12Z")

</div>

On 12 May 2008, at 21:46, Jeff M. wrote:

> Hello,  
> I have a hash map that gets pumped full of info. The key is a  
> username… I’m using a @myhashmap.each do |key, item| to display the  
> hash map in my rhtml, but I need it to be alphabetized… Is there a  
> built-in way to do this?

hashes in ruby don’t have a guaranteed order (at least not in 1.8.  
Ruby 1.9 is different). Your best bet is probably to sort the keys and  
iterate on that.

Fred

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 12, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412/3 "2008-05-12T23:06:41Z")

</div>

On Mon, May 12, 2008 at 10:46:46PM +0200, Jeff M. wrote:

> Hello,  
> I have a hash map that gets pumped full of info. The key is a  
> username… I’m using a @myhashmap.each do |key, item| to display the  
> hash map in my rhtml, but I need it to be alphabetized… Is there a  
> built-in way to do this?

@myhashmap.sort.each do |key, item| …

> Thanks,
> 
> - Jeff M.  
> –Greg

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 12, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412/4 "2008-05-12T23:12:07Z")

</div>

sort worked great, thanks!

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [May 12, 2008, 11:28pm UTC](https://www.ruby-forum.com/t/organizing-a-hash-map/137412/5 "2008-05-12T23:28:58Z")

</div>

On 5/12/08, Jeff M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> I have a hash map that gets pumped full of info. The key is a  
> username… I’m using a @myhashmap.each do |key, item| to display the  
> hash map in my rhtml, but I need it to be alphabetized… Is there a  
> built-in way to do this?

gem install orderedhash

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
