Why is Array.sort() failing on large arrays? Did I overlook something? I have double-checked to ensure that I'm not accidentally sorting strings. I'm struggling to comprehend why this is failing
Here is the code snippet I pasted from Chrome Debugger's Watch:
https://i.sstatic.net/VZmdS.png
Feel free to play around with this code:
arr = [1.238648000000012,1.5776880000000233,1.6462280000000078,10.007896000000017,10.014455999999996,10.016970000000015,10.018888000000004,10.028069000000016,10.047926000000018,10.056818000000021,10.057980999999984,10.075353000000007,10.086242000000027,10.088684999999998 ...
output = document.getElementById("output");
output.innerHTML += Math.max(...arr) + '<br />';
output.innerHTML += arr.sort()[arr.length-1] + '<br />';
output.innerHTML += arr.sort()[arr.length-2] + '<br />';
output.innerHTML += arr.sort()[0] + '<br />';
output.innerHTML += '---------------------------------------' + '<br />'
a = [1,2,3,0];
output.innerHTML += Math.max(...a) + '<br />';
output.innerHTML += a.sort()[a.length-1] + '<br />';
output.innerHTML += a.sort()[a.length-2] + '<br />';
output.innerHTML += a.sort()[0] + '<br />';
<html>
<body>
<div id='output'></div>
</body>
</html>
Is it not expected for the last element in the sorted array to be the maximum value?