MCPcopy Index your code
hub / github.com/TanStack/db / createKeyedComparator

Function createKeyedComparator

packages/db-ivm/src/operators/topKArray.ts:28–41  ·  view source on GitHub ↗
(
  comparator: (a: T, b: T) => number,
)

Source from the content-addressed store, hash-verified

26 * then uses the row key as a stable tie-breaker.
27 */
28export function createKeyedComparator<K extends string | number, T>(
29 comparator: (a: T, b: T) => number,
30): (a: [K, T], b: [K, T]) => number {
31 return ([aKey, aVal], [bKey, bVal]) => {
32 // First compare on the value
33 const valueComparison = comparator(aVal, bVal)
34 if (valueComparison !== 0) {
35 return valueComparison
36 }
37 // If the values are equal, use the row key as tie-breaker
38 // This provides stable, deterministic ordering since keys are string | number
39 return compareKeys(aKey, bKey)
40 }
41}
42
43export type TopKChanges<V> = {
44 /** Indicates which element moves into the topK (if any) */

Callers 2

constructorMethod · 0.85
constructorMethod · 0.85

Calls 2

comparatorFunction · 0.85
compareKeysFunction · 0.85

Tested by

no test coverage detected