I have to sort this array,
const array = [
[18, [18, 16], 16],
15,
[18, 19, 51],
[[18, 16], 15, [14, 13]],
[10, 9, 20],
99,
49,
[11, [22, 10], [[10, 9], 11, 9, [1, 2]], 100, 72],
[[11], [[19, 14], [77, 18]]]
];
sort this array such that,
-
all single elements inside this array come first
-
after that one-dimensional arrays come second
-
after that two-dimensional arrays come third
-
after that three-dimensional arrays come fourth
-
…..so on
-
all elements should be sorted inside all the arrays.
Result should be like this,
[
15,
49,
99,
[18, 19, 51],
[9, 10, 20],
[16, 18, [16, 18]],
[15, [16, 18], [13, 14]],
[11, 72, 100, [10, 22], [9, 11, [9, 10], [1, 2]]],
[[11], [[14, 19], [18, 77]]]
]
This is how I try to implement it – it is working for up to four-dimensional arrays, but this code will not work if we have n-dimensional array(were n>4), so how can we implement/optimise this code such that we can get result for all n-dimensional arrays.
const array = [
[ 18, [18,16], 16],
15,
[18, 19, 51],
[[18, 16], 15, [14, 13]],
[10,9,20],
99,
49,
[11, [22,10], [[10,9], 11, 9, [1,2]], 100, 72],
[[11], [[19, 14], [77, 18]]]
];
function getArrayDepth(value) {
return Array.isArray(value) ?
1 + Math.max(0, ...value.map(getArrayDepth)) :
0;
}
function sort_array1(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
function sort_array2(arr) {
for (let j = 0; j < arr.length; j++) {
arr[j] = sort_array1(arr[j]);
}
return arr;
}
function sort_array3(arr) {
for (let j = 0; j < arr.length; j++) {
let array1 = [];
let array2 = [];
for (let k = 0; k < arr[j].length; k++) {
if (getArrayDepth(arr[j][k]) == 0) {
array1.push(arr[j][k]);
}
else {
array2.push(arr[j][k]);
}
}
array1 = sort_array1(array1);
array2 = sort_array2(array2);
arr[j] = array1.concat(array2);
}
return arr;
}
function sort_array4(arr) {
for (let j = 0; j < arr.length; j++) {
let array1 = [];
let array2 = [];
let array3 = [];
for (let k = 0; k < arr[j].length; k++) {
if (getArrayDepth(arr[j][k]) == 0) {
array1.push(arr[j][k]);
}
else if (getArrayDepth(arr[j][k]) == 1) {
array2.push(arr[j][k]);
}
else {
array3.push(arr[j][k]);
}
}
array1 = sort_array1(array1);
array2 = sort_array2(array2);
array3 = sort_array3(array3);
arr[j] = array1.concat(array2, array3);
}
return arr;
}
function sequenced_array(arr) {
let array1 = [];
let array2 = [];
let array3 = [];
let array4 = [];
for (let i = 0; i < arr.length; i++) {
if (getArrayDepth(arr[i]) == 0) {
array1.push(arr[i]);
}
else if (getArrayDepth(arr[i]) == 1) {
array2.push(arr[i]);
}
else if (getArrayDepth(arr[i]) == 2) {
array3.push(arr[i]);
}
else {
array4.push(arr[i]);
}
}
array1 = sort_array1(array1);
array2 = sort_array2(array2);
array3 = sort_array3(array3);
array4 = sort_array4(array4);
array1 = array1.concat(array2, array3, array4);
return array1;
}
let sortedArray = sequenced_array(array);
/*for (let i = 0; i < sortedArray.length; i++) {
console.log(sortedArray[i]);
}*/
console.log(sortedArray)
3
Answers
I solved this using recursion,
Thanks! Everyone for your efforts to solve my question.
NOTE: The custom sort is quite heavy. This many have both a time and space complexity that may be prohibitive for very large and/or deep arrays.
Perhaps others may give a better solution