What is the principle of sort() method in JavaScript and how is it implemented?
仅有的幸福
仅有的幸福 2017-06-12 09:22:48
0
2
1035

sort() sorts the array and replaces the original array elements. But how is it implemented? Is it through bubbling? Quick sort or some other sorting method? Ask for advice~

[9, 3, 4, 6, 1, 2, 8, 7].sort(function(x, y) {
    return x - y;
})

Result: [1, 2, 3, 4, 6, 7, 8, 9]

————————————————————————————

[9, 3, 4, 6, 1, 2, 8, 7].sort(function(x, y) {
    return y-x;
})

Result: [9, 8, 7, 6, 4, 3, 2, 1]

仅有的幸福
仅有的幸福

reply all(2)
给我你的怀抱

/q/10...

刘奇

Different browser implementations may be different, and the sorting method may also be different depending on the number of array elements. I remember there is an article about this, search it first and then ask

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template