From: Joel VanderWerf Date: 2006-10-30T03:09:12+09:00 Subject: Re: keeping a hash' sort order? ara.t.howard@noaa.gov wrote: ... > 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]} -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407