stack

Home > @infiniteobjects/core-library-web > Arrays > sortBy

Arrays.sortBy() method

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[];

Parameters

Parameter Type Description
collection T[]  
iteratee (item: T) => R  
comparer (x: R, y: R) => number (Optional)

Returns:

T[]

Remarks

It returns a copy of the array

Example

let array: string[] = [ 'aaa', 'bb', 'c' ];
Arrays.sortBy(array, x => x.length);  // [ 'c', 'bb', 'aaa' ]