From: Francis Cianfrocca Date: 2006-08-15T07:44:38+09:00 Subject: Re: A use case for an ordered hash On 8/14/06, Bil Kleb wrote: > Here's my latest one[1]: > > I had a string with a bunch of tagged "tolerance fields", > e.g., >>1+/-10% 'tag'<< (a mini DSL?). Based on some sort > of distribution function, e.g., uniform or Gaussian, > I generated an array of samples for each tagged field. > A regular hash was a perfect fit, > > samples[tag] = [ sample1, sample2, .. ] > > *until* another requirement came up: to present the tags > in order of appearance. I've had to deal with this (interestingly, in something that was *intended* as a DSL). Just add another field to the structure you store in the hash and have it increment a global sequence number when you create it. A little clunky but later you can say: hash.values.sort {|a,b| a.seq <=> b.seq}.each {|v| # whatever }