I have an array with subarrays and wish to sort numerically by the first item in the subarrays.
Using sort() function:
arr = [[2,'text'],[5,'text'],[1,'text'],[3,'text'],[6,'text'],[4,'text'],
[7,'text'],[9,'text'],[3,'text'],[1,'text'],[8,'text']];
arr.sort(function(a, b) {
return a[0] < b[0];
});
I found that if the array contains less than 10 subarrays, the sort() function work good. But if the array contains more than 10 subarrays, then something goes wrong.
In this case, the array has 11 items, so the result generated by Chrome is:
[4, "text"]
[8, "text"]
[9, "text"]
[7, "text"]
[6, "text"]
[5, "text"]
[3, "text"]
[3, "text"]
[2, "text"]
[1, "text"]
[1, "text"]
The 4 should be at the middle of the array, not on the top.
Any idea why it happened? Thanks.