我一直试图快速使用sortInPlace函数,但是它不起作用。当我使用sort函数而不是sortinplace时,它可以工作。

请说明这两个功能之间的区别。如果您可以提供小代码示例来演示这两个功能的使用,那将非常有帮助。

最佳答案

var mutableArray = [19, 7, 8, 45, 34]

// function sort sorts the array but does not change it. Also it has return

mutableArray.sort()

mutableArray
// prints 19, 7, 8, 45, 34

// function sortInPlace will mutate the array. Do not have return

mutableArray.sortInPlace()

mutableArray
// prints 7, 8, 19, 34, 45

10-05 21:06