Does .sort function change original array?

前端 未结 3 1949
忘了有多久
忘了有多久 2020-12-03 10:52

I have that code:

arr = arr.sort(function (a, b) {
    return a.time>b.time
})

Do I need to redefine arr or it is possible just to call

3条回答
  •  爱一瞬间的悲伤
    2020-12-03 11:27

    It sorts the array in place (modifying the array). From MDN:

    The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

提交回复
热议问题