From: Robert Klemme Date: 2006-10-29T20:35:20+09:00 Subject: Re: keeping a hash' sort order? 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. > 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| ... 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