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.

10th Dec 2020, 8:14 PM
MyNameIsMutable
MyNameIsMutable - avatar
4 ответов
+ 1
sort() function works.. L = [[1, 0], [0, 0], [2, 2],[0,1]] L.sort() console.log(L)
10th Dec 2020, 8:45 PM
Jayakrishna 🇮🇳
+ 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
10th Dec 2020, 10:19 PM
Ali Kh
Ali Kh - avatar
+ 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.
11th Dec 2020, 3:52 PM
MyNameIsMutable
MyNameIsMutable - avatar
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******
11th Dec 2020, 3:56 PM
Ali Kh
Ali Kh - avatar