Cho mảng lẫn số và chuỗi, viết hàm sắp xếp tách 2 loại và sắp xếp trong mỗi loại.
Ví dụ
const arr = [1, 5, 'fd', 6, 'as', 'a', 'cx', 43, 's', 51, 7]; const sorter = (a, b) => { const first = typeof a === 'number'; const second = typeof b === 'number'; if(first && second){ return a - b; }else if(first && !second){ return -1; }else if(!first && second){ return 1; }else{ return a > b ? 1 : -1; } }; arr.sort(sorter); console.log(arr);Kết quả
Đầu ra console:
[ 1, 5, 6, 7, 43, 51, 'a', 'as', 'cx', 'fd', 's' ]