-
Notifications
You must be signed in to change notification settings - Fork 0
/
Double Sort
18 lines (13 loc) · 841 Bytes
/
Double Sort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
Simple enough this one - you will be given an array. The values in the array will either be numbers or strings, or a mix of both. You will not get an empty array, nor a sparse one.
Your job is to return a single array that has first the numbers sorted in ascending order, followed by the strings sorted in alphabetic order. The values must maintain their original type.
Note that numbers written as strings are strings and must be sorted with the other strings.
*/
function dbSort(a){
//sort values of the array in ascending order
//sort strings in alphabetical order
//return an array with nums first, then strings
let sortedNums = a.filter(a => typeof a === 'number').sort((a,b) => a - b)
let sortedLetters = a.filter(a => typeof a === 'string').sort((a,b) => a.localeCompare(b))
return [...sortedNums, ...sortedLetters]
}