Understanding of Javacript Sort () method

Baidu Here are many such article, also written very good, but for arguments like this:

Here's a, b has not understood, why use an anonymous function, a representative of what, b represent?

Then their understanding becomes:

a, b can be seen as an array of two adjacent number of budget anonymous function as a parameter, then a similar bubble sort, like, every time and do comparison until all adjacent contrast finish.

ab> 0 is the exchange order (descending)

ab <0 is the same position (ascending)

a -b = 0 relative positions unchanged

var array = [1,2,3,4,56];
array.sort((a,b)=>a-b);

Reference article:

https://www.cnblogs.com/saifei/p/9043821.html

https://www.cnblogs.com/ainyi/p/9651714.html

Watching will understand a little better.

 

Guess you like

Origin www.cnblogs.com/rovingkite/p/11265884.html