Rush StackShopBlogEvents
Skip to main content

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

Sort.sortSetBy() method

Sorts the entries in a Set object according to the specified keys. The result is guaranteed to be a stable sort.

Signature:

static sortSetBy<T>(set: Set<T>, keySelector: (element: T) => any, keyComparer?: (x: T, y: T) => number): void;

Parameters

ParameterTypeDescription
setSet<T>
keySelector(element: T) => any
keyComparer(x: T, y: T) => number(Optional)

Returns:

void

Example

let set: Set<string> = new Set<string>();
set.add('aaa');
set.add('bb');
set.add('c');
Sort.sortSetBy(set, x => x.length);
console.log(Array.from(set)); // ['c', 'bb', 'aaa']