标签:foreach includes turn length sam asc class ret rip
let arr = [7, 1, 24, 1, 0, 4, 27, 20] function toOrder(arr) { let newArr = [] arr.forEach((a, i) => { let samll = newArr[0] let big = newArr[newArr.length - 1]; if (samll == undefined) { newArr.push(a) } else if (samll > a) { newArr = [a, ...newArr]; } else if (big < a) { newArr = [...newArr, a]; } else if (newArr.includes(a)) { //去重 } else { for (let index = 0; index < newArr.length - 1; index++) { if (a >= newArr[index] && a <= newArr[index + 1]) { newArr = (newArr[index] = [newArr[index], a]) && [].concat(...newArr); break } } } }); return newArr } console.log(toOrder(arr))
应该算的次数少一点吧 大概
标签:foreach includes turn length sam asc class ret rip
原文地址:https://www.cnblogs.com/lieaqi/p/10535356.html