- desc表示降序,asc表示升序
const arr = [1, 3, 5, 76, 88, 9, 6, 7, 5, 7, 100, 57];
function sortFunc (data, order) {
let sortArr = [];
while (arr.length) {
const maxItem = order === 'desc' ? Math.max(...arr) : Math.min(...arr);
const index = data.findIndex(item => item === maxItem);
sortArr = sortArr.concat(data.splice(index, 1));
}
return sortArr;
}
const sortArr = sortFunc(arr, 'asc');
console.log("sortArr===>", sortArr)