0
How would I sort arrays of an array?
For ex. how would I sort [[1, 0], [0, 0], [2, 2]] in ascending order? This is based on [X, Y] coordinates so the numbers must stay the same. If there is anything I can clarify I will be glad to elaborate.
4 ответов
+ 1
sort() function works..
L = [[1, 0], [0, 0], [2, 2],[0,1]]
L.sort()
console.log(L)
+ 4
Jayakrishna🇮🇳 , in this case .. I guess the arrays will be sorted as strings (internal coercion) not numbers .. right?
I am thinking to provide a call back function to control the sorting process
+ 1
Ali Kh Doing so I could be able to control if the Xs are prioritized of the Ys. If I find something interesting as such I will post here.
0
MyNameIsMutable
We can do it like that if we want to sort based on the first element of all sub arrays (X)
------------- version 1 ---------
L.sort(function (sub1, sub2){
return sub2[0] - sub1[1];
})
-------- shoerter version ----;;;
L.sort((sub1, sub2) => sub2[0] - sub1[1] );
edited******