Home > @microsoft/node-core-library > Sort > sortBy

Sort.sortBy() method

Sorts the array according to a key which is obtained from the array elements.

Signature:

static sortBy<T>(array: T[], keySelector: (element: T) => any, comparer?: (x: any, y: any) => number): void;

Parameters

Parameter Type Description
array T[]  
keySelector (element: T) => any  
comparer (x: any, y: any) => number  

Returns:

void

Example

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