Skip to main content

Function: isSortedByKey()

isSortedByKey<ElementType, MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (a: ElementType, index: number) => MappedType, comparator: CompareByComparator<MappedType>): boolean

Checks if the elements of this iterator are sorted using the given key extraction function.

Instead of comparing the iterator's elements directly, this function compares the keys of the elements, as determined by callbackFn. Apart from that, it's equivalent to isSorted; see its documentation for more information.

Type Parameters

Type Parameter
ElementType
MappedType

Parameters

ParameterTypeDefault valueDescription
iterableIterableResolvable<ElementType>undefinedThe iterator to compare.
callbackFn(a: ElementType, index: number) => MappedTypeundefinedThe function to extract the key from an element.
comparatorCompareByComparator<MappedType>defaultCompare-

Returns

boolean

Seealso

isSorted for a version that uses the default comparator.

Seealso

isSortedBy for a version that allows custom comparators.

Example

import { isSortedByKey } from '@sapphire/iterator-utilities';

assert(isSortedByKey(['c', 'bb', 'aaa'], (s) => s.length));
assert(!isSortedBy([-2, -1, 0, 3], (n) => Math.abs(n)));

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/isSortedByKey.ts:33