此代码为什么起作用?对数组进行排序
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});
var x = document.getElementById("demo");
x.innerHTML=points;
}
</script>
</body>
</html>
它是由W3Schools
采取我不理解的points.sort(function(a,b){return a-b});
部分。
我不明白'points.sort(功能(A,B){返回A-B});'一部分。 –
没有W3Schools解释Array.sort是如何工作的? –
它使用内置函数sort()根据传递的函数对数字进行排序,在本例中为'function(a,b){return a-b}'。 – DrCord