How does JavaScript's sort (compareFunction) work? [duplicate]
问题 This question already has answers here : How does Javascript's sort() work? (7 answers) How does sort function work in JavaScript, along with compare function (5 answers) Closed 3 years ago . var arr = [5, 2, 1, -10, 8]; arr.sort(function(a, b) { console.log(a,b) return b - a; }) ; // 8, 5, 2, 1, -10 How does this callback work? What is the principle of choice a and b? Please explain this particular example from the inside. output console.log (at first, please explain this output ): 5 2 2 1 1