Q:

why does array.sort{|x,y| y <=> x} sort the array in descending order in Ruby? 

I recently came across a piece of code that used
array.sort{|x,y| y <=> x}

to sort an array of integers in descending order.  I looked up the <=> operator and understand that it returns three different values, -1, 0, or 1, depending on whether or not one value is less than, greater to, or equal to the other other value. 
But I cannot reason out why this would make the above code sort the array in descending order, but know that it definitely does thanks to IRB.  What's going on here?  Can somebody explain this to me? 

A:

For the same reasons that this:
array.sort{|x,y| x <=> y}

Sorts by ascending order. 

Take this program for example:
[1,5,2,4,3].sort do |x,y|
  puts "---"
  puts x
  puts y
  puts x <=> y
  x <=> y
end

It'll output the two numbers that it's comparing, and then the result of the <=> during the sort.  It outputs this:
---
1
2
-1 # 1 is less than 2
---
2
3
-1 # 2 is less than 3
---
5
2
1 # 5 is greater than 1
---
4
2
1 # 4 is greater than 2
---
5
4
1 # 5 is greater than 4
---
4
3
1 # 4 is greater than 3

If you reverse the order of x <=> y to be y <=> x, you're going to get the opposite result.