// 字符串数组中红黄蓝三种球排序 // 红蓝蓝黄红黄蓝红红黄红 => 黄黄黄红红红红红蓝蓝蓝 const fn = (arr) => { let yellow = [], red = [], blue = [] arr.forEach(item => { switch (item) { case '黄': yellow.push(item) break case '红': red.push(item) break case '蓝': blue.push(item) break } }) return yellow.concat(red, blue) } console.log(fn('红蓝蓝黄红黄蓝红红黄红'.split('')))