📜  function(a, b){return b - a} - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:49.902000             🧑  作者: Mango

代码示例1
The sort() method needs to know the relation between each two elements in order to sort them. 
For each pair (according to the algorithm used) it calls the function then based on the return value,
may swap them. 
Note that a-b returns a negative value if b>a and a positive one if a>b. 
That leads to an ascending order.

Just for test, replace a-b with b-a and you will get a descending order. 
You may even make it more complicated,
 sort them based on (for example) their least significant digit:

a.sort(function() {return (a%10) - (b%10);}