-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.js
36 lines (28 loc) · 818 Bytes
/
p1.js
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
/**
* Input:
* let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"];
*
* Output:
* ["3", "4", "10", "23", "42", "63", "ABC", "adidas", "ax", "ba", "mof", ")", "["]
*
*/
function customSort(arr) {
//Digit only
let nums = arr.filter((v) => /^\d+$/.test(v));
//String of alphabet chars
let strs = arr.filter((v) => /^[a-zA-Z]+$/.test(v));
//remain
let spes = arr.filter((v) => /[^a-zA-Z0-9]+/.test(v));
nums = nums.map(v => parseInt(v))
nums = nums.sort((a, b) => a - b);
nums = nums.map(v => ""+v)
strs = strs.sort()
spes.sort()
return [...nums, ...strs, ...spes];
}
;
(() => {
let arr = ["ax", "mof", "4", "63", "42", "3", "10", "[", "23", "adidas", "ba", ")", "ABC"];
const result = customSort(arr)
console.log(result)
})()