On Thu, Mar 25, 2010 at 3:58 PM, Augusto G. [email protected] wrote:
result != 0 ? result : b[:silvercount] <=> a[:silvercount]
result != 0 ? result : b[:bronzcount] <=> a[:bronzcount]
result != 0 ? result : b[:country] <=> a[:country]
end
I assume you have an array of hashes:
irb(main):001:0> country_medals = []
=> []
irb(main):002:0> country_medals << {:country => “Spain”, :total =>
100, :goldcount => 100, :silvercount => 0, :bronzecount => 0}
=> [{:country=>“Spain”, :total=>100, :goldcount=>100, :silvercount=>0,
:bronzecount=>0}]
irb(main):003:0> country_medals << {:country => “US”, :total => 50,
:goldcount => 50, :silvercount => 0, :bronzecount => 0}
=> [{:country=>“Spain”, :total=>100, :goldcount=>100, :silvercount=>0,
:bronzecount=>0}, {:country=>“US”, :total=>50, :goldcount=>50,
:silvercount=>0, :bronzecount=>0}]
irb(main):004:0> country_medals << {:country => “UK”, :total => 50,
:goldcount => 45, :silvercount => 5, :bronzecount => 0}
=> [{:country=>“Spain”, :total=>100, :goldcount=>100, :silvercount=>0,
:bronzecount=>0}, {:country=>“US”, :total=>50, :goldcount=>50,
:silvercount=>0, :bronzecount=>0}, {:country=>“UK”, :total=>50,
:goldcount=>45, :silvercount=>5, :bronzecount=>0}]
irb(main):010:0> country_medals.sort_by {|x| [-x[:total],
-x[:goldcount], -x[:silvercount], -x[:bronzecount], x[:country]]}
=> [{:country=>“Spain”, :total=>100, :goldcount=>100, :silvercount=>0,
:bronzecount=>0}, {:country=>“US”, :total=>50, :goldcount=>50,
:silvercount=>0, :bronzecount=>0}, {:country=>“UK”, :total=>50,
:goldcount=>45, :silvercount=>5, :bronzecount=>0}]
Jesus.