Skip to main content

Function: compareBy()

compareBy<ElementType>(iterable: IterableResolvable<undefined | ElementType>, other: IterableResolvable<undefined | ElementType>, comparator: CompareByComparator<ElementType>): LexicographicComparison

Lexicographically compares the elements of both iterators are equal. That is:

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<undefined | ElementType>The iterator to compare.
otherIterableResolvable<undefined | ElementType>The iterator to compare against.
comparatorCompareByComparator<ElementType>-

Returns

LexicographicComparison

Whether the two iterators are equal.

Example

import { ascNumber, compareBy } from '@sapphire/iterator-utilities';

const x = [1, 2, 3, 4];
const y = [1, 4, 9, 16];
console.log(compareBy(x, y, (x, y) => ascNumber(x, y)));
// Output: -1
console.log(compareBy(x, y, (x, y) => ascNumber(x * x, y)));
// Output: 0
console.log(compareBy(x, y, (x, y) => ascNumber(x * 2, y)));
// Output: 1

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/compareBy.ts:30