[英]What's the most efficient way to merge multiple arrays of sorted objects in JavaScript?
Consider four arrays of objects all sorted individually by date 考虑四个按日期分别排序的对象数组
Object: { id: userId, date : date }
对象: { id: userId, date : date }
Using JavaScript how should I sort these lists into one merged list by date? 如何使用JavaScript将这些列表按日期排序到一个合并的列表中?
Here's a fiddle with the four lists individually pre sorted. 这是一个小提琴 ,其中四个列表分别进行了预先排序。
If you'll definitely have the arrays passed to you sorted, the most efficient thing I can think of is to make your own merge algorithm. 如果绝对可以将传递给您的数组排序,那么我想到的最有效的方法就是创建自己的合并算法。 It would be something like this: 就像这样:
var merged = [];
var arrIndex1 = 0;
var arrIndex2 = 0;
var arrIndex3 = 0;
var arrIndex4 = 0;
while (arrIndex1 < arr1.length || arrIndex2 < arr2.length || arrIndex3 < arr3.length || arrIndex4 < arr4.length) {
var val1 = arrIndex1 < arr1.length ? arr1[arrIndex1].date : Number.POSITIVE_INFINITY;
var val2 = arrIndex2 < arr1.length ? arr2[arrIndex2].date : Number.POSITIVE_INFINITY;
var val3 = arrIndex3 < arr1.length ? arr3[arrIndex3].date : Number.POSITIVE_INFINITY;
var val4 = arrIndex4 < arr1.length ? arr4[arrIndex4].date : Number.POSITIVE_INFINITY;
if (val1 < val2 && val1 < val3 && val1 < val4) {
merged.push(arr1[arrIndex1++]);
} else if (val2 < val2 && val1 < val3 && val1 < val4) {
merged.push(arr2[arrIndex2++]);
} else if (val3 < val2 && val1 < val3 && val1 < val4) {
merged.push(arr3[arrIndex3++]);
} else {
merged.push(arr4[arrIndex4++]);
}
}
This would be the fastest way. 这将是最快的方法。 However, the easiest way to code it - if you're not concerned about it being the fastest - is simply to splice
the four arrays together, and run them through your activites.sort()
function. 但是,最简单的编码方法-如果您不担心它是否最快-只是将四个数组splice
在一起,然后通过activites.sort()
函数运行它们。
Because I've got something hard to do and need to procrastinate :-) here's a function "mergeSortedArrays". 因为我很难做,所以需要拖延:-)这是一个函数“ mergeSortedArrays”。
It can take any number of parameters, so you call it as: 它可以采用任意数量的参数,因此您将其称为:
var resultSorted = mergeSortedArrays(resultA, resultB, resultC, resultD);
It's not as good as it could be, because it only merges two arrays at a time. 它不尽如人意,因为它一次只能合并两个数组。 Perhaps the best implementation would simultaneously merge all the arrays at once. 也许最好的实现是同时合并所有数组。 (I'm not sure how that would compare though) (我不确定该如何比较)
The implementation: 实现:
function mergeSortedArrays() {
function merge(arrayOne, arrayTwo) {
var totalLength = arrayOne.length + arrayTwo.length;
var returnArray = new Array(totalLength);
var iResult = 0;
var iOne = 0;
var iTwo = 0;
for(var i = 0; i < totalLength; ++i) {
if(iTwo < arrayTwo.length) {
if(iOne >= arrayOne.length) {
returnArray[i] = arrayTwo[iTwo++];
} else if (arrayOne[iOne].date < arrayTwo[iTwo].date) {
returnArray[i] = arrayOne[iOne++];
} else {
returnArray[i] = arrayTwo[iTwo++];
}
} else {
returnArray[i] = arrayOne[iOne++];
}
}
return returnArray;
}
var sortedArray = [];
for(var i = 0; i < arguments.length; ++i) {
sortedArray = merge(sortedArray, arguments[i]);
}
return sortedArray;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.