/**
* @description: 排序数字英文字母交错,由小到大
* @param {type} 如:[1,’a’,2,’b’,3,’c’,’d’,’e']
* @return:
*/
const array = ['z', 's', 'g', 'h', 'i', 'j', 'f', 's', 16, 66, 36, 616, 6, '', 0];
const test4 = (arr = []) => {
let newAry = []; // 新数组
let lowAry = arr.sort(); // 老数组直接排序
let numAry = lowAry.filter((item) => {
return typeof item == 'number';
}).sort((a, b) => a - b); // number数组
let letterAry = lowAry.filter((item) => {
return item && typeof item == 'string';
}); // 字母数组
let maxAry = numAry.length > letterAry.length ? numAry : letterAry; // 获取最长数组,循环使用
maxAry.forEach((item, index) => {
// 防止undefined,push进去
if (numAry[index] || numAry[index] === 0) {
newAry.push(numAry[index]);
}
// 防止undefined,push进去
if (letterAry[index]) {
newAry.push(letterAry[index]);
}
});
return newAry;
};
let aa = test4(array);
console.log(aa);
排序数字英文字母交错,由小到大
最新推荐文章于 2022-04-10 22:09:38 发布