keeping a hash' sort order?

D

dc

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
 
R

Robert Klemme

dc said:
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
 
A

ara.t.howard

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?)


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
 
T

Trans

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?)


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:

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.
 
J

Joel VanderWerf

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]

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]}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top