From: Robert Klemme Date: 2011-06-23T02:37:21+09:00 Subject: Re: How to order a hash based on its keys? On Wed, Jun 22, 2011 at 4:19 PM, Iñaki Baz Castillo wrote: > 2011/6/22 Robert Klemme : >> Hm...  Now I am not sure any more whether you need a associative >> container at all.  Now we have three steps.  From what I understand >> you do: >> >> 1. items are collected >> 2. items are ordered by priority (what you call second container above) >> 3. items are ordered by some randomization algorithm >> >> I'm obviously missing other access operations out here.  So far I >> don't see any need for a Hash or similar structure.  What else is it >> that you need to do with these items?  What properties do they have? > > > Step 1) > > I get data (DNS SRV records), which is an array with entries like this: > >  # @priority=1, @weight=50, @port=5062> > > Each entry is a SRV record. It has a priority (lowest value is the > best) and weight (higher value means more probability to choose it). > > The client getting such array of SRV records wants to connect to a > server based on SRV priorities/weight, which works as follows: > - SRV records with best priority must be used first. > - If there are various SRV records with same priority, then the > probability of choosing each one depends on its weight value (random > with weight). > - If the chosen SRV record fails to connect (i.e. server down) next > one (based on same priority/weight rules) must be tryed. Aha! This is how I'd probably do it. https://gist.github.com/1040631 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/