-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsortedList.js
More file actions
26 lines (23 loc) · 885 Bytes
/
sortedList.js
File metadata and controls
26 lines (23 loc) · 885 Bytes
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
// Zaimplementuj funkcję przyjmującą jako argument tablicę dowolnych elementów.
// W przypadku gdy jest to string lub liczba, element taki zostaje dodany do tablicy wyjściowej.
// Pozostałe typy nie są uwzględniane w tablicy wyjściowej. Tak utworzoną tablicę należyposortować
// w kolejności: cyfry/liczby od najmniejszej do największej, litery/słowa od Z do A.
// input: [12, 3, 'das', 'pol', {}, null]
// output: [3, 12, 'pol', 'das']
// solution
function mySort(arr) {
const sortedNumbers = [];
const sortedStrings = [];
arr.map((item) => {
if (typeof item === "number") {
sortedNumbers.push(item);
}
if (typeof item === "string") {
sortedStrings.push(item);
}
});
return sortedNumbers
.sort((a, z) => a - z)
.concat(sortedStrings.sort().reverse());
}
console.log(mySort([5, 12, 3, "das", "pol", "pod", {}, null]));