Skip to main content

Function: isSorted()

isSorted<ElementType>(iterable: IterableResolvable<ElementType>): boolean

Defined in: projects/utilities/packages/iterator-utilities/src/lib/isSorted.ts:36

Checks if the elements of this iterator are sorted in ascending order.

That is, for each element a and its following element b, a <= b must hold. If the iterator yields exactly zero or one element, true is returned.

This function uses the default comparator (lexicographically), which means it will compare the elements as strings. If this is undesirable, use isSortedBy instead.

Type Parameters​

Type Parameter
ElementType

Parameters​

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to compare.

Returns​

boolean

Seealso​

isSortedBy for a version that allows custom comparators.

Seealso​

isSortedByKey for a version that allows custom key extractors.

Example​

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

assert(isSorted([1, 2, 2, 9]);
assert(!isSorted([1, 3, 2, 4]);

assert(isSorted([0]);
assert(isSorted([]);
assert(isSorted([0, 1, NaN]);

Remarks​

This function consumes the entire iterator.