<=>
returns only 1, 0, or -1 regardless of difference between two operands:
1 <=> 7 # => -1
1 <=> 5 # => -1
3 <=> 2 # => 1
3 <=> 0 # => 1
You'd better to use -
operator instead.
In addition to that, sort
, sort_by
yield little value first. To list higher score (win - loss
) first, you need negate the score.
Inaddition to that, to get exact same output you want, you need format the output using String%#
or sprintf
:
board.sort_by { |key,value| value['Loss'] - value['Win'] }.each do |key, value|
puts "%-10s %2s %3s" % [key, value['Win'], value['Loss']]
end
output:
Patriots 3 0
Broncos 1 1
Steelers 0 1
Colts 0 2
UPDATE
This answer is based on wrong assumption about the rank. Please see sawa's answer.