Rush StackShopBlogEvents
Skip to main content

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

Sort.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>(array: T[], keySelector: (element: T) => any, comparer?: (x: any, y: any) => number): void;

Parameters

ParameterTypeDescription
arrayT[]
keySelector(element: T) => any
comparer(x: any, y: any) => number(Optional)

Returns:

void

Example

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