# Preserve insert order in a Hash

**URL:** <https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922>\
**Category:** Ruby\
**Created:** [September 18, 2008, 11:50am UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922 "2008-09-18T11:50:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 11:50am UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/1 "2008-09-18T11:50:26Z")

</div>

Hi,  
I would like to know if it’s possible to insert values in a Hash and  
then extract all of them in the same insertion order.

I noticed that if I then try to print the content of an Hash using  
“each” the order is not the same as the insertion.

Thanks

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 11:58am UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/2 "2008-09-18T11:58:01Z")

</div>

From: Me Me [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I would like to know if it’s possible to insert values in a Hash and

# then extract all of them in the same insertion order.

only in ruby 1.9

C:\ruby1.9\bin\>irb.bat

> h={}  
> =\> {}

> h[1]=1  
> =\> 1

> h[2]=1  
> =\> 1

> h[3]=1  
> =\> 1

> h  
> =\> {1=\>1, 2=\>1, 3=\>1}

> h[1.5]=1  
> =\> 1

> h  
> =\> {1=\>1, 2=\>1, 3=\>1, 1.5=\>1}

> h[0]=1  
> =\> 1

> h  
> =\> {1=\>1, 2=\>1, 3=\>1, 1.5=\>1, 0=\>1}

> RUBY\_VERSION  
> =\> “1.9.0”

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/3 "2008-09-18T12:01:56Z")

</div>

PeÃ±a, Botp wrote:

> From: Me Me [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # I would like to know if it’s possible to insert values in a Hash and
> 
> # then extract all of them in the same insertion order.
> 
> only in ruby 1.9

Could anybody explain why this feature was added? Isn’t it going to slow  
down the operations on the Hash? I think it is useless to mix Array with  
Hash.

TPR.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:21pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/4 "2008-09-18T12:21:01Z")

</div>

Thomas B. wrote:

> PeÃ±a, Botp wrote:
> 
> > From: Me Me [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> > 
> > # I would like to know if it’s possible to insert values in a Hash and
> > 
> > # then extract all of them in the same insertion order.
> > 
> > only in ruby 1.9
> 
> Could anybody explain why this feature was added? Isn’t it going to slow  
> down the operations on the Hash? I think it is useless to mix Array with  
> Hash.
> 
> TPR.

well, basically I just need a Hash to use String as indexes like

sourceInfo = Hash.new  
sourceInfo[“var1”]=123  
sourceInfo[“var2”]=2  
sourceInfo[“var3”]=3  
sourceInfo[“var4”]=23

and then print them in the exact order of insertion

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/5 "2008-09-18T12:29:55Z")

</div>

Dick D. wrote:

> On Thu, Sep 18, 2008 at 11:13 AM, Me Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > Hash.
> > 
> > and then print them in the exact order of insertion
> 
> Can’t you just sort them on the way out?  
> i.e.
> 
> sourceinfo.keys.sort.each { |k|  
> sourceinfo[k].do\_stuff  
> }

thanks  
well in this case they will be sorted alphabetically, right?  
the example is simplyfied, in my case the keys have differernt names.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/6 "2008-09-18T12:26:49Z")

</div>

On Thu, Sep 18, 2008 at 11:13 AM, Me Me [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Hash.
> 
> and then print them in the exact order of insertion

Can’t you just sort them on the way out?  
i.e.

sourceinfo.keys.sort.each { |k|  
sourceinfo[k].do\_stuff  
}

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/7 "2008-09-18T12:34:38Z")

</div>

unknown wrote:

> a =   
> a.push 1  
> a.push 2  
> a.push 3  
> a.each {|e|puts e}

I think this is not my case, I need a Hash as I explained

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/8 "2008-09-18T12:32:42Z")

</div>

a = []  
a.push 1  
a.push 2  
a.push 3  
a.each {|e|puts e}

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/9 "2008-09-18T12:36:49Z")

</div>

Hi,

In message “Re: Preserve insert order in a Hash”  
on Thu, 18 Sep 2008 18:54:20 +0900, “Thomas B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Could anybody explain why this feature was added?

Useful for some cases, especially for keyword arguments.

|Isn’t it going to slow down the operations on the Hash?

No. hash reference operation does not touch order information, only  
for iteration. Memory consumption increased a bit.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/10 "2008-09-18T14:50:18Z")

</div>

> well, basically I just need a Hash to use String as indexes like
> 
> sourceInfo = Hash.new  
> sourceInfo[“var1”]=123  
> sourceInfo[“var2”]=2  
> sourceInfo[“var3”]=3  
> sourceInfo[“var4”]=23

What do you want to happen if sourceInfo[“var2”] is assigned a second  
time? Do you want to replace it where it originally was in the sequence,  
or do you want to delete it and add the new value to the end? Or do you  
want both elements to appear at the same time? Or doesn’t it matter?

I am just wondering because perhaps all you need is

sourceInfo =   
sourceInfo \<\< [“var1”,123]  
sourceInfo \<\< [“var2”,2]  
sourceInfo \<\< [“var3”,3]  
sourceInfo \<\< [“var4”,4]  
sourceInfo.each { |k,v| puts “#{k}=\>#{v}” }

At least, that’s fine if all you want to do is iterate over the  
collection and add new elements. Finding or deleting elements by key  
requires a linear search:

dummy,value = sourceInfo.find { |k,v| k == “var2” }

However you can optimise this by building a hash as you go which points  
to the same elements; or you could build an array containing just the  
keys.

class HashAndArray  
def initialize  
@h, @a = {},   
end  
def   
@h[k]  
end  
def =(k,v)  
@a \<\< k unless @h.has\_key?(k)  
@h[k] = v  
end  
def each  
@a.each { |k| yield k,@h[k] }  
end  
end

sourceInfo = HashAndArray.new  
sourceInfo[“var1”]=123  
sourceInfo[“var2”]=2  
sourceInfo[“var3”]=3  
sourceInfo[“var4”]=23  
sourceInfo.each { |k,v| puts “#{k}=\>#{v}” }  
puts sourceInfo[“var2”]

Adding a delete() method is left as an exercise.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 1:46pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/11 "2008-09-18T13:46:19Z")

</div>

From: “Me Me” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I think this is not my case, I need a Hash as I explained

Here’s an implementation for ruby 1.8 … I apologize for the lack of  
tests.

class InsertOrderPreservingHash  
include Enumerable

def initialize(\*args, &block)  
@h = Hash.new(\*args, &block)  
@ordered\_keys =   
end

def =(key, val)  
@ordered\_keys \<\< key unless @h.has\_key? key  
@h[key] = val  
end

def each  
@ordered\_keys.each {|k| yield(k, @h[k])}  
end  
alias :each\_pair :each

def each\_value  
@ordered\_keys.each {|k| yield(@h[k])}  
end

def each\_key  
@ordered\_keys.each {|k| yield k}  
end

def keys  
@ordered\_keys  
end

def values  
@ordered\_keys.map {|k| @h[k]}  
end

def clear  
@ordered\_keys.clear  
@h.clear  
end

def delete(k, &block)  
@ordered\_keys.delete k  
@h.delete(k, &block)  
end

def reject!  
del =   
each\_pair {|k,v| del \<\< k if yield k,v}  
del.each {|k| delete k}  
del.empty? ? nil : self  
end

def delete\_if(&block)  
reject!(&block)  
self  
end

%w(merge!).each do |name|  
define\_method(name) do |\*args|  
raise NotImplementedError, “#{name} not implemented”  
end  
end

def method\_missing(\*args)  
@h.send(\*args)  
end  
end

# example:

h = InsertOrderPreservingHash.new

h[:aaa] = 0  
h[:foo] = 123  
h[:bar] = 456  
h[:baz] = 789  
h.delete :aaa  
h[:aaa] = 1

h.each\_pair {|k,v| p [k,v]}

# produces:

[:foo, 123]  
[:bar, 456]  
[:baz, 789]  
[:aaa, 1]

Regards,

Bill

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/12 "2008-09-18T14:52:24Z")

</div>

On Thu, Sep 18, 2008 at 6:28 AM, Yukihiro M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> |Isn’t it going to slow down the operations on the Hash?
> 
> No. hash reference operation does not touch order information, only  
> for iteration. Memory consumption increased a bit.

If I remember correctly, it’s been a while since I looked at the code,  
1.9  
implements this by using a singly linked list which introduces a small  
overhead only when elements are either added or deleted.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/13 "2008-09-18T14:55:21Z")

</div>

Hi,

Yukihiro M. wrote in [ruby-talk:315237]:  
At Thu, 18 Sep 2008 19:28:28 +0900,

> |Could anybody explain why this feature was added?
> 
> Useful for some cases, especially for keyword arguments.

And the performance of iterations improved a little, but  
significantly.

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/14 "2008-09-18T15:11:49Z")

</div>

Hi,

In message “Re: Preserve insert order in a Hash”  
on Thu, 18 Sep 2008 21:44:23 +0900, “Rick DeNatale”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|\> No. hash reference operation does not touch order information, only  
|\> for iteration. Memory consumption increased a bit.  
|\>  
|  
|If I remember correctly, it’s been a while since I looked at the code, 1.9  
|implements this by using a singly linked list which introduces a small  
|overhead only when elements are either added or deleted.

You’re right. Thank you for pointing out.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/15 "2008-09-18T17:24:19Z")

</div>

Bill K. wrote:

> From: “Me Me” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > I think this is not my case, I need a Hash as I explained
> 
> Here’s an implementation for ruby 1.8 … I apologize for the lack of  
> tests.

Thanks a lot! That worked perfectly  
Bye

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/16 "2008-09-18T15:18:52Z")

</div>

On Thu, Sep 18, 2008 at 8:42 AM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> or do you want to delete it and add the new value to the end? Or do you  
> want both elements to appear at the same time? Or doesn’t it matter?

FWIW, Ruby 1.9 seems to keep the original insertion order, reassiging a  
value to an existing key leaves the order unchanged:

$ irb1.9  
irb(main):001:0\> a = {a: 1, b: 2, c: 3}  
=\> {:a=\>1, :b=\>2, :c=\>3}  
irb(main):002:0\> a.keys  
=\> [:a, :b, :c]  
irb(main):003:0\> a[:b] = 4  
=\> 4  
irb(main):004:0\> a.keys  
=\> [:a, :b, :c]  
irb(main):005:0\> a  
=\> {:a=\>1, :b=\>4, :c=\>3}  
irb(main):006:0\>

At least my somewhat out of date 1.9 does

$ ruby1.9 -v  
ruby 1.9.0 (2008-03-21 revision 0) [i686-darwin9.2.2]

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/17 "2008-09-18T17:57:28Z")

</div>

On Sep 18, 2008, at 3:42 AM, Me Me wrote:

> 

gem install orderedhash

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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 18, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/18 "2008-09-18T21:23:48Z")

</div>

If scale is needed this has been implemented by Takuma Ozawa as a very  
efficient RB tree which I used on a Mac:

sudo gem install rbtree  
irb  
require ‘rbtree’  
a=RBTree.new  
a[‘x’]=1  
a[‘z’]=2  
a[‘y’]=3  
a  
============\> #RBTree:{‘x’=\>1,‘y’=\>3,‘z’=\>2}

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 19, 2008, 12:11am UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/19 "2008-09-19T00:11:40Z")

</div>

On Sep 18, 2008, at 10:01 AM, jim finucane wrote:

> a  
> cfp:~ \> cat a.rb  
> require ‘rubygems’  
> require ‘rbtree’

# rbtree is sorted by the keys ‘natural’ sort order

# _not_ the insertion order

rb = RBTree.new

rb[‘z’] = 3  
rb[‘y’] = 2  
rb[‘x’] = 1

rb.each do |key, value|  
puts “#{ key } : #{ value }”  
end

cfp:~ \> ruby a.rb  
x : 1  
y : 2  
z : 3

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

---

<div class="post-metadata">

**Author:** ![melmoth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9de053/32.png) [@melmoth](https://www.ruby-forum.com/u/melmoth)\
**Post date:** [September 19, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/preserve-insert-order-in-a-hash/148922/20 "2008-09-19T17:55:59Z")

</div>

PeÃ±a, Botp wrote:

> From: Me Me [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # I would like to know if it’s possible to insert values in a Hash and
> 
> # then extract all of them in the same insertion order.
> 
> only in ruby 1.9

Well, that is not _entirely_ true.

h={}  
p h  
h[1]=1  
p h  
h[2]=1  
p h  
h[3]=1  
p h  
h  
p h  
h[1.5]=1  
p h  
h  
p h  
h[0]=1  
p h  
h  
p h

yields:

{}  
{1=\>1}  
{1=\>1, 2=\>1}  
{1=\>1, 2=\>1, 3=\>1}  
{1=\>1, 2=\>1, 3=\>1}  
{1=\>1, 2=\>1, 3=\>1, 1.5=\>1}  
{1=\>1, 2=\>1, 3=\>1, 1.5=\>1}  
{1=\>1, 2=\>1, 3=\>1, 1.5=\>1, 0=\>1}  
{1=\>1, 2=\>1, 3=\>1, 1.5=\>1, 0=\>1}

in JRUBY 1.1 which I am running on windows under netbeans.
