-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_the_unsortable.js
More file actions
38 lines (29 loc) · 1.58 KB
/
Sort_the_unsortable.js
File metadata and controls
38 lines (29 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// ----------------------------------------Description----------------------------------------------
// In this challenge you will be given an array similar to the following:
// [[3], 4, [2], [5], 1, 6]
// In words, elements of the array are either an integer or an array containing a single integer. We humans can clearly see that this array can reasonably be sorted according to "the content of the elements" as:
// [1, [2], [3], 4, [5], 6]
// Create a function that, given an array similar to the above, sorts the array according to the "content of the elements".
// Examples:
// sortIt([4, 1, 3]) ➞ [1, 3, 4]
// sortIt([[4], [1], [3]]) ➞ [[1], [3], [4]]
// sortIt([4, [1], 3]) ➞ [[1], 3, 4]
// sortIt([[4], 1, [3]]) ➞ [1, [3], [4]]
// sortIt([[3], 4, [2], [5], 1, 6]) ➞ [1, [2], [3], 4, [5], 6]
// Notes
// To reiterate, elements of the array will be either integers or arrays with a single integer.
// ---------------------------------------------------------------------------------------------------
const sortIt = (arr) => {
return arr.sort((a, b) => {
const valA = Array.isArray(a) ? a[0] : a;
const valB = Array.isArray(b) ? b[0] : b;
return valA - valB;
});
}
// Examples
console.log(sortIt([4, 1, 3])); // ➞ [1, 3, 4]
console.log(sortIt([[4], [1], [3]])); // ➞ [[1], [3], [4]]
console.log(sortIt([4, [1], 3])); // ➞ [[1], 3, 4]
console.log(sortIt([[4], 1, [3]])); // ➞ [1, [3], [4]]
console.log(sortIt([[3], 4, [2], [5], 1, 6])); // ➞ [1, [2], [3], 4, [5], 6]
console.log(sortIt([[-3], 14, [202], [55], 1, 16])); // ➞ [ [ -3 ], 1, 14, 16, [ 55 ], [ 202 ] ]