let array = [7, 1, 2, 9, 8, 4]; function insert(array) { let arr = []; arr.push(array[0]); for (let i = 1; i < array.length; i++) { let inserted = false; for (let j = arr.length - 1; j >= 0; j--) { if (array[i] >= arr[j]) { arr.splice(j + 1, 0, array[i]); inserted = true; break; } } if (!inserted) { arr.splice(0, 0, array[i]); } } return arr; } console.log(insert(array));