Skip to main content

Function: sorted()

sorted<ElementType>(iterable: IterableResolvable<ElementType>, compareFn?: (a: ElementType, b: ElementType) => number): IterableIterator<ElementType>

Consumes the iterable and returns a new iterable with the elements sorted.

Type parameters

Type parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to sort.
compareFn?(a: ElementType, b: ElementType) => numberA function that defines the sort order. If omitted, the values are sorted in ascending order.

Returns

IterableIterator<ElementType>

An iterator that yields the values of the provided iterator in sorted order.

Example

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

const iterable = [5, 3, 1, 4, 2];
console.log([...sorted(iterable)]);
// Output: [1, 2, 3, 4, 5]

Remarks

This function consumes the entire input iterator.

Source

projects/utilities/packages/iterator-utilities/src/lib/sorted.ts:24