a,b两个有序数组,将b合并到a,形成一个有序数组
var target = [6, 5]
var input = [2, 5, 7]
function joinArr(a, b) {
var order = 1;//正序
for (var i = 0; i < a.length; i++) {
if (i >= 1 && (a[i] - a[i - 1]) < 0) {
order = -1;//倒序
break;
}
}
b.sort((b1,b2)=>{
return order * (b1 - b2);
})
var index = 0;
for (var i = 0; i < b.length; i++) {
for (var j = index; j < a.length; j++) {
if ((order === 1 && b[i] - a[j] <= 0) || (order === -1 && b[i] - a[j] >= 0)) {
a = [...a.slice(0, j), b[i], ...a.slice(j)]
index = j;
break;
}
if (j === a.length - 1) {
index = a.length;
}
}
if (index === a.length) {
a = [...a, ...b.slice(i)]
break;
}
}
return a;
}
target = joinArr(target, input)
target
//[7, 6, 5, 5, 2]