a <=> b
will return -1
if a
belongs before b
, 0
if they're equal, or 1
if a
should follow b
.
b <=> a
will return -1
if b
belongs before a
, 0
if they're equal, or 1
if b
should follow a
.
Since you are reversing the order, the output should be reversed, just like the -
operator, for example. 3-5
is -2
, and 5-3
is 2
.
array.sort { |b, a| b <=> a }
is equal to array.sort { |a, b| a <=> b }
because the first argument is before the spaceship, and the second is after. Ruby doesn't care what the name of the variable is.