From: Aleksandar Lazic Date: 2006-10-31T03:45:09+09:00 Subject: Re: How can i do 'foreach (reverse sort { $hasref->{$a} <=> $hasref->{$b} } keys %{$hasref})..' in ruby On Mon 30.10.2006 10:18, Daniel Sheppard wrote: >> For the archive: >> >> My working line is know: >> >> clients.sort{|(k1,v1),(k2,v2)| v2 <=> v1}.each{|k,v| >> printf("% 7i %s\n",v, k) >> } > >For the simple case (such as yours) where the values being sorted are >numeric, I find it neater to just do: [snipp] >Or: > >clients.sort_by{|k,v| v}.reverse_each {|k,v| > printf("% 7i %s\n",v, k) >} Cool ;-))) >Which is probably clearest in intention (looking at the original code >you post, it's not immediately apparent the it's a reverse sort). These >methods have the added benefit of also being the faster code (sort_by >is much faster than sort). You are right but I have hoped that a perl person understand my intetion and point me to the right ruby solution ;-)) Thanks for your info and time. Aleks