According to Array.prototype.sort() description:
Since version 10 (or EcmaScript 2019), the specification dictates that
Array.prototype.sort
is stable.
It won't swap [a,b]
if a == b
. It also won't swap them if a < b
.
Then a short function like this looks valid and universal:
arr.sort( (a,b) => a>b )
And before that a short valid one was:
arr.sort( (a,b) => a>b||-1 )
Is that a correct and universal way to sort an array? Or am I missing something, and it won't work in some cases?
(By "universal" I mean it works for any array type: array of numbers, array of strings...)