# Access Hash in the same order that was created

**URL:** <https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721>\
**Category:** Ruby\
**Created:** [May 15, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721 "2008-05-15T18:22:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/1 "2008-05-15T18:22:51Z")

</div>

I need to access to a Hash in the same order that It was created:

mh=Hash.new()  
mh[“one”]=“1”  
mh[“two”]=“2”  
mh[“three”]=“3”  
mh[“four”]=“4”

mh.each {|val|  
puts val[0]  
}

In this example I get:  
three  
two  
one  
four

and I would like to get:  
one  
two  
three  
four

is that possible?

Thanks

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:29pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/2 "2008-05-15T18:29:55Z")

</div>

Mario R. wrote:

> I need to access to a Hash in the same order that It was created:  
> is that possible?
> 
> Thanks

No.

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/3 "2008-05-15T18:34:08Z")

</div>

On May 15, 2008, at 10:22 AM, Mario R. wrote:

> }  
> three  
> four
> 
> is that possible?
> 
> Thanks

gem install orderedhash

mh = OrderedHash.new  
…  
…

etc.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:37pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/4 "2008-05-15T18:37:20Z")

</div>

ara.t.howard wrote:

> is that possible?

Hash preserves insertion order in Ruby 1.9

irb(main):026:0\> RUBY\_VERSION  
=\> “1.9.0”  
irb(main):027:0\> mh.each{|val| puts val[0]}  
one  
two  
three  
four

regards,  
Tor Erik

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/5 "2008-05-15T18:35:20Z")

</div>

On May 15, 2008, at 10:22 AM, Mario R. wrote:

> is that possible?

forgot to mention - 1.9 does this by default now.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/6 "2008-05-15T18:40:40Z")

</div>

On Fri, May 16, 2008 at 01:22:56AM +0900, Mario R. wrote:

> I need to access to a Hash in the same order that It was created:  
> […]  
> is that possible?

First off, this is not a hash. Hashes are inherently unordered. Hashes  
provide amortized O(1) insertion and retrieval of elements by key, and  
that’s it. If you need an ordered set of pairs, use an array of arrays.  
Yes, this is a pet peeve of mine.

There was an OrderedHash that someone wrote, but needing it is an  
indication of a design problem. It has also been obsoleted by the  
Dictionary in Ruby F.s.

One could argue that, like Java, there should be a standard Dictionary  
interface that Hash implements, and that there should be another  
implementation that preserves order without guaranteeing anything about  
performance. That’s not how things stand right now.

> Thanks  
> –Greg

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/7 "2008-05-15T19:37:51Z")

</div>

- Mario R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (18:22) schrieb:

> }  
> three  
> four
> 
> is that possible?

The Hash doesn’t remember the order in which you put the values in.

If the order is important not easily reconstructable, you have to  
store the keys in an Array.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 16, 2008, 11:52am UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/8 "2008-05-16T11:52:03Z")

</div>

Thanks to everybody

---

<div class="post-metadata">

**Author:** ![tcblues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/db5fbb/32.png) [@tcblues](https://www.ruby-forum.com/u/tcblues)\
**Post date:** [May 15, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/access-hash-in-the-same-order-that-was-created/137721/9 "2008-05-15T18:41:51Z")

</div>

> mh[“two”]=“2”  
> one  
> four
> 
> and I would like to get:  
> one  
> two  
> three  
> four
> 
> is that possible?

No way with standard Hash, and it shouldn’t be really. You can use an  
array instead of a hash, and Array#assoc to retrieve entries based on  
keys.

Gennady.
