Swift: Can someone explain this syntax `numbers.sort { $0 > $1 }` for me?
<p>First of all, this question is not about "what does $0 mean". I learnt in swift document that $0 is like index. </p>

<p>My question is "How <code>numbers.sort { $0 &gt; $1 }</code> can be used to implement a sort function". I searched for this syntax <code>numbers.sort { $0 &gt; $1 }</code> in some other websites, for example <a href="https://blog.mariusschulz.com/2014/09/14/playing-with-closure-expressions-and-operator-functions-in-swift" rel="noreferrer">this one</a>. It is apparently not the current version. So I still can't understand what the meaning of it.</p>

<pre><code>print(numbers) //[20, 19, 1, 12]
let sortedNumbers = numbers.sort { $0 &gt; $1 }
print(sortedNumbers) //[20, 19, 12, 1]
</code></pre>

<p>Can someone explain this simple piece of code above for me? Like how this simple code <code>$0 &gt; $1</code> implement the sort function, sorting the numbers from big to small. </p>

<p>I know some about index, and this <strong>$0</strong> looks like index, but it only has <strong>$0</strong> and <strong>$1</strong> two indices. So how can it be used into 4 numbers? According to my knowledge in C++ before, I can't understand the principle in this.</p>

<p>Please make your answer as <em>specific</em> as possible. Thank you!</p>

<p>----------------- Below is edited extra part -------------------</p>

<p>I don't know whether stackoverflow would allow me to edit my question like this, but this extra part is too long, so I can't add it in the comment. 
@pbodsk @Paul Richter </p>

<p>So the sort() syntax in swift uses quick sort to deal with sort function? </p>

<p>Actually my question is more about "what is the <em>operating principle</em> of <code>sort{$0 &gt; $1}</code>". I know what you mean above, and I think it's similar with what swift 2.1 document says, but your answer is not what I really want to know. Sorry, my English expression is not very good. Let me try another way. </p>

<p>When I learnt C++ before, there are always some documents to explain what a function's <em>operating principle</em> is or how this function (like <strong>sort()</strong> here) operate in background. Sort() here needs to compare first and second interchange. In C++, it's like </p>

<pre><code>if numbers[1] &lt; numbers[2]{ //just consider this pseudocode
    int k;
    k = numbers[1];
    numbers[1] = numbers[2];
    numbers[2] = k;
}
</code></pre>

<p>We can see this process is obvious. In swift, it's like </p>

<pre><code>numbers.sort({(val1: Int, val2: Int) -&gt; Bool in
   return val1 &gt; val2
})
</code></pre>

<p>Where is it compared? And how is it interchanged? Does <code>return val1 &gt; val2</code> automatically compare and interchange these two values and return them? Just this one syntax implement these all 3 processes? How? This is what I really want to know. Sorry again for my poor English expression.</p>