From: "Simon Kröger" Date: 2005-12-09T05:15:43+09:00 Subject: Re: ordered/sorted hash robertj wrote: > hi, > > the only real requirement is > that #each returns me the > key value pairs in an ordered fashion. > > default ordering should be by key. > > one could but think of also having > a switch that allows for ordering by > value. > > ciao robertj hi, if speed isn't an issue: --------------------------------------------- class SortedHash < Hash alias :unsorted_each :each def each keys.sort.each{|k| yield k, self[k]} end end h = SortedHash[*Array.new(20){rand(100)}] h.each{|k, v| puts "#{k} => #{v}"} --------------------------------------------- this isn't meant to replace a red black tree implementation of course. cheers Simon