From: Piotr Szotkowski Date: 2011-06-23T00:02:31+09:00 Subject: Re: How to order a hash based on its keys? --YZ5djTAD1cGYuMQK Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable I=C3=B1aki Baz Castillo: > I get data (DNS SRV records), which is an array with entries like this: > # > 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. Something to the tune of srvs.sort { |a, b| (a.priority <=3D> b.priority).nonzero? or (b.weight <=3D= > a.weight).nonzero? or rand } should work, then. :) =E2=80=94 Piotr Szotkowski --=20 =E2=80=98Driving myself slowly nuts with printing recursive S-expressions s= anely in Go.=E2=80=99 =E2=80=98Ford Prefect always said that going insane should be enjoyed, and is not something to be rushed.=E2=80=99 [Eleanor McHugh, Phillip Gawlo= wski] --YZ5djTAD1cGYuMQK Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iEYEARECAAYFAk4CA/8ACgkQi/mCfdEo8UpMkACbBAqOOyuGGbi2PX7bCkQQh2Cd oMoAoIyZdFJ2I+MY6FPU7fn+9wcrRyNB =oHG9 -----END PGP SIGNATURE----- --YZ5djTAD1cGYuMQK--