Skip to content

Function.sortBy

github-actions[bot] edited this page Nov 18, 2026 · 13 revisions

umt / sortBy

Function: sortBy()

sortBy<T>(array, ...selectors): T[]

Defined in: Array/sortBy.ts:17

Sorts an array by one or more key selectors with a stable TimSort Each selector is called once per element. Later selectors break ties left by earlier ones, and elements whose keys are all equal keep their original order. Keys are compared with compareFunctionDefault, so incomparable keys such as NaN compare as equal.

Type Parameters

T

T

Parameters

array

T[]

The array to sort (not mutated)

selectors

...((value) => unknown)[]

Functions returning the sort keys, in priority order

Returns

T[]

A new sorted array

Examples

sortBy([{ age: 30 }, { age: 20 }], (user) => user.age); // [{ age: 20 }, { age: 30 }]
sortBy(["bb", "a", "ab"], (s) => s.length, (s) => s); // ["a", "ab", "bb"]

API

Classes

Interfaces

Type Aliases

Variables

Functions

Clone this wiki locally