简体   繁体   English

在JavaScript中合并多个排序对象数组的最有效方法是什么?

[英]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;
}

Here's the jsFiddle 这是jsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 合并两个对象数组的最有效方法 - Most efficient way to merge two arrays of objects 将排序的集合合并为新的排序的集合的最有效方法? - Most efficient way to merge sorted collections into a new sorted collection? 在 JavaScript 中将 N 个未排序数组合并为 1 个排序数组的最快方法是什么? - What is the fastest way to merge N unsorted arrays into 1 sorted array in JavaScript? 在操作数类型为 Arrays 的 JavaScript 中乘法、加法、减法和除法的最有效方法是什么? - What's the most efficient way to multiply, add, subtract, and divide in JavaScript where the operands are Typed Arrays? Javascript:按键求和多个数组的最有效方法 - Javascript: Most Efficient Way of Summing Multiple Arrays by Key 比较两个对象的值的最有效方法是什么? - What's the most efficient way to compare values of two objects? 转换对象的 arrays 的最有效方法 - Most efficient way to transform arrays of objects 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 在JavaScript中,将标签转换为链接的最有效方法是什么? - In javascript, what's the most efficient way to I turn tags into links?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM