kMerge

Function

k-way merge implementation that merges (and potentially sorts) k individually sorted arrays based on the k-way merge algorithm (https://en.wikipedia.org/wiki/K-way_merge_algorithm)

If a comparator function used to compare data is provided, a k-way merge sort is performed using a Binary Heap implementation. Otherwise data from each input array is merged incrementally

The returned object contains merged (and potentially sorted) data, as well as key-value pairs that represent the index of a result set in the "data" parameter (the key) tied to how many results (the value) of that result set were added to the final merged data array. For example, if "data" represents an array of two result sets, with 3 and 5 results added to the final result set, respectively, the returned object would be { data: [...], "0": 3, "1": 5 }

  • kMerge(data: [][], resultLimit: number, cmptr: comparator<[]>, direction: HeapDirection) : {
    data:
    []
    } [key: string]: any

Parameters

Parameter Type Default Notes
data Required [][]

An array of result sets, each an array of type T

resultLimit Optional number 10

the maximum number of merged results to return, defaults to 10

cmptr Optional comparator<[]>

comparator function that takes in two instances of type T and returns a negative number if a is less than b, a positive if a is greater than b, 0 if equal

direction Optional HeapDirection

specifies whether data should be ordered ascending or descending

Returns

list of results and key-value pairs indicating how many from each were added to returned data

{
data:
[]
} [key: string]: any

Function defined in search/src/util/merge-sort/merge.ts:34