I need to organize 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]]]
];
Arrange this array in the following way:
All single elements within the array should come first
Next, include one-dimensional arrays
Then, two-dimensional arrays
And so on for higher dimensions
Ensure all elements are sorted within each sub-array.
The expected result should look 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 implementation works up to four-dimensional arrays. However, it won't handle arrays with dimensions greater than four (n > 4). How can we optimize the code to achieve results for any n-dimensional 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]]]
];
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)