Home > @infiniteobjects/core-library-web > Arrays > sortBy
Sorts the array according to a key which is obtained from the array elements. The result is guaranteed to be a stable sort.
Signature:
static sortBy<T, R = unknown>(collection: T[], iteratee: (item: T) => R, comparer?: (x: R, y: R) => number): T[];
Parameter | Type | Description |
---|---|---|
collection | T[] | |
iteratee | (item: T) => R | |
comparer | (x: R, y: R) => number | (Optional) |
Returns:
T[]
It returns a copy of the array
let array: string[] = [ 'aaa', 'bb', 'c' ];
Arrays.sortBy(array, x => x.length); // [ 'c', 'bb', 'aaa' ]