Given a multidimensional, rectangular array of nonnegative integers, sort it at every depth (lexicographically), starting from the innermost.
For example, with this array:
[ [ [5, 1, 4],
[10, 7, 21] ],
[ [9, 20, 2],
[4, 2, 19] ] ]
You'd sort at the deepest first:
[ [ [1, 4, 5],
[7, 10, 21] ],
[ [2, 9, 20],
[2, 4, 19] ] ]
Then sort at the next depth, lexicographically:
[ [ [1, 4, 5],
[7, 10, 21] ],
[ [2, 4, 19],
[2, 9, 20] ] ]
lexicographic comparison of arrays means comparing the first element, and if they're equal comparing the second, and so on all the way down the line. Here we see that [2, 4, 19]
is less than [2, 9, 20]
because although the first elements are equal (2) the second aren't - 4 < 9.
Finally, sort at the top depth (lexicographically):
[ [ [1, 4, 5],
[7, 10, 21] ],
[ [2, 4, 19],
[2, 9, 20] ] ]
The first one is less than the second, because [1, 4, 5]
is less than [2, 4, 19]
because 1 is less than 2.
You may take the lengths of dimensions and/or depth of the array as well.
Testcases
[2, 1] -> [1, 2]
[[[10, 5, 9], [6, 4, 4]], [[2, 6, 3], [3, 3, 2]], [[3, 8, 6], [1, 5, 6]]] -> [[[1, 5, 6], [3, 6, 8]], [[2, 3, 3], [2, 3, 6]], [[4, 4, 6], [5, 9, 10]]]
[[[6, 9], [12, 17]], [[9, 6], [9, 8]]] -> [[[6, 9], [8, 9]], [[6, 9], [12, 17]]]
[[[9, 1], [2, 5]], [[8, 5], [3, 5]]] -> [[[1, 9], [2, 5]], [[3, 5], [5, 8]]]
0x10ffff
? \$\endgroup\$