0

Looking at this: http://jsfiddle.net/Y5t45/

<!DOCTYPE html>
<html>
<body>

<p>Click the button to sort the array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
var points = [40, 100, 1, 5, 25, 10, 30, 60, 50, 70, 12, 14, 56, 33];
document.getElementById("demo").innerHTML = points;

function myFunction() {
    points.sort(function(a, b){return 0});
    document.getElementById("demo").innerHTML = points;
}
</script>

</body>
</html>

Can anyone tell me why if you keep pressing 'try it' the first 10 items of the Array continually change, but not the rest?

I'm assuming this is related to comparison being done as Unicode and not numeric, but it still doesn't mean the items should be shifting around since the return 0 should mean they're all equal.

In addition, what is the code to make this sort numerically as expected?

automaton
  • 1,972
  • 5
  • 25
  • 40

0 Answers0