This is a simplification of what's happening and not the sorting algorithm Ruby uses (hopefully), but it might help understand what's happening.
Let's say you have this array of ages you want to sort:
[15, 25, 12]
Now the sorting algorithm starts going through the array to determine the correct ordering. First it picks the first two elements and compares them:
|15, 25| 15 <=> 25
The result is -1, so now it knows that 15 should be sorted before 25.
Next it takes 25 and 12.
|25, 12| 25 <=> 12
The result is 1. 12 should be sorted before 25 as well.
Now we know that 25 should be sorted after every other element. We only need to know the order between 15 and 12.
|15, 12| 15 <=> 12
The result is 1, so 12 should be sorted before 15. Now the final ordering is known:
[12, 15, 25]
As you see, the comparison algorithm is called multiple times. x
and y
in |x, y|
are placeholders for array elements that are passed to the comparison method.