search

Home  >  Q&A  >  body text

What is the principle of sort() method in JavaScript and how is it implemented?

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]

仅有的幸福仅有的幸福2724 days ago999

reply all(2)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-12 09:24:48

    /q/10...

    reply
    0
  • 怪我咯

    怪我咯2017-06-12 09:24:48

    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

    reply
    0
  • Cancelreply