Skip to main content

sort

sort() function

Returns an array, sorted according to the comparator f, which should accept two values

Signature:
declare function sort<T>(f: (a: T, b: T) => unknown, iterable: Iterable<T>): T[];

declare function sort(f: (a: any, b: any) => unknown, iterable: readonly []): any[];

declare function sort<T>(f: (a: T, b: T) => unknown, iterable: AsyncIterable<T>): Promise<T[]>;

declare function sort<T extends Iterable<unknown> | AsyncIterable<unknown>>(f: (a: IterableInfer<T>, b: IterableInfer<T>) => unknown): (iterable: T) => ReturnValueType<T, IterableInfer<T>[]>;

Example

sort((a, b) => a > b, [3, 4, 1, 2, 5, 2]); // [1, 2, 2, 3, 4, 5]
sort((a, b) => a.localeCompare(b), "bcdaef"); // ["a", "b", "c", "d", "e", "f"]

Open Source Code