In Javascript, if I have an array of arrays, for example:
X = [ [1,2,3,4],
[1,1,2,3],
[1,1,3],
[1,4],
[2,1,2],
[2,2]
]
Javascript sorts my array, comparing the first record first, then the second, and so on, so it X.sort()returns the following:
[ [1,1,2,3],
[1,1,3],
[1,2,3,4],
[1,4],
[2,1,2],
[2,2]
]
This is what I want. The problem is that the comparison operator for comparing elements in arrays is lexicographical, therefore, [10,2] < [2,2]and, for example,
[[10,2],[1,1,3],[2,2]].sort() -> [[1,1,3],[10,2],[2,2]]
I need to sort it numerically, so that I get a sorted array [[1,1,3],[2,2],[10,2]].
I tried to use a comparison function function(a,b){return (a-b) }that will work for sorting an array of numbers, but this does not allow me to sort my array correctly, which makes sense (I think) because it [10,2] - [1,1,3]givesNaN
How do I sort an array of numeric arrays?