es-stl - v3.0.2
    Preparing search index...
    • Merges two sorted arrays into a new sorted array.

      Type Parameters

      • T

        Element type stored in the arrays.

      Parameters

      • array1: T[]

        First sorted input array.

      • array2: T[]

        Second sorted input array.

      • callback: SortCallback<T>

        Sort comparator shared by both input arrays.

      Returns T[]

      A new array containing all values in sorted order.

      Both input arrays must already be sorted with the same comparator. The returned array is a new array; neither input array is mutated. When the comparator treats two values as equal, the value from array2 is emitted first.

      const merged = mergeSortedArray([1, 4], [2, 3], (a, b) => a - b);
      // merged === [1, 2, 3, 4]
      const left = [{ time: 10 }, { time: 30 }];
      const right = [{ time: 20 }];

      const merged = mergeSortedArray(left, right, (a, b) => a.time - b.time);
      // merged.map((item) => item.time) === [10, 20, 30]