# Keeping a hash' sort order?

**URL:** <https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042>\
**Category:** Ruby\
**Created:** [October 29, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042 "2006-10-29T12:06:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 12:06pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042/1 "2006-10-29T12:06:41Z")

</div>

hi -

sure this is a common question…  
i have a hash of stuff that i want to load/keep in a particular order.

irb(main):005:0\> hsh = { “one” =\> 1, “two” =\> 2, “three” =\> 3,  
“four”=\>4, “twentythree”=\>23 }

gives back:  
=\> {“three”=\>3, “two”=\>2, “twentythree”=\>23, “one”=\>1, “four”=\>4}

ruby sorts the hash in apparent random order (by object id?)

how can i keep the items in the order i want?

someone said i should probly not use a hash at all? however, i do want  
to keep the items organized in the order like above, but access thru  
hashkeys.

hsh.each\_pair{ |tag, val| …

can i use a more complex structure and sort by some internal key?

thanks!

## /dc

```
  David "DC" Collier
  +81 (0)80 6521 9559
  skype: callto://d3ntaku
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 12:36pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042/2 "2006-10-29T12:36:43Z")

</div>

dc wrote:

> hi -
> 
> sure this is a common question…

Yes, and that is the exact reason why I suggest you search through the  
archives before posting.

> how can i keep the items in the order i want?
> 
> someone said i should probly not use a hash at all? however, i do want  
> to keep the items organized in the order like above, but access thru  
> hashkeys.
> 
> hsh.each\_pair{ |tag, val| …

That’s not an access by hash key but an iteration. For that nested  
arrays are sufficient.

> can i use a more complex structure and sort by some internal key?

As said, please go to the archives. There are numerous postings and  
implementations available.

Regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 2:56pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042/3 "2006-10-29T14:56:50Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > gives back:  
> > rb = RBTree.new  
> > end  
> > harp:~ \> ruby a.rb  
> > [“a”, 1]  
> > [“b”, 2]  
> > [“c”, 3]
> 
> if you want to sort on insertion order use an ordered hash:

Facets’ Dictionary class can handle arbitray order via #insert(i,k,v),  
as well as automatic sort orders (though it is not as fast as rbtree).

T.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 7:11pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042/4 "2006-10-29T19:11:31Z")

</div>

removed\_email\_address@domain.invalid wrote:  
…

> ```
> super value
> rb[Key("c", 1)] = 3
> 
> rb.each{|k,v| p [k,v]}
> 
> harp:~ > ruby a.rb
> ["a", 1]
> ["b", 2]
> ["c", 3]
> 
> ```

What’s the ‘arbitrary’ part for? The Key#\<=\> method is not called. The  
following has the same output:

```
 rb = RBTree.new

 rb["a"] = 1
 rb["b"] = 2
 rb["c"] = 3

 rb.each{|k,v| p [k,v]}
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 29, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/keeping-a-hash-sort-order/74042/5 "2006-10-29T14:34:49Z")

</div>

On Sun, 29 Oct 2006, dc wrote:

> ruby sorts the hash in apparent random order (by object id?)

if you really want to sort on arbitrary keys use an rbtree:

```
 harp:~ > cat a.rb
 require 'rbtree' # rubyforge or raa

 rb = RBTree.new

 class Key < ::String
   attr 'arbitrary'
   def initialize value, arbitrary
     super value
     @arbitrary = arbitrary
   end
   def <=> other
     self.arbitrary <=> other.arbitrary
   end
 end
 def Key(*a, &b) Key.new(*a, &b) end

 rb[Key("a", 3)] = 1
 rb[Key("b", 2)] = 2
 rb[Key("c", 1)] = 3

 rb.each{|k,v| p [k,v]}

 harp:~ > ruby a.rb
 ["a", 1]
 ["b", 2]
 ["c", 3]

```

if you want to sort on insertion order use an ordered hash:

```
 harp:~ > cat a.rb
 require 'alib' # rubyforge or raa
 require 'yaml'

 oh = alib.orderedhash.new

 oh['a'] = 1
 oh['b'] = 2
 oh['c'] = 3

 y oh

 harp:~ > ruby a.rb
 a: 1
 b: 2
 c: 3

```

more examples and explantions abound on the list if you want to google  
them.

-a
