For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/sortWith.md.

sortWith

Sorts an array using multiple comparators in priority order.

The first comparator has highest priority. When it returns 0 (tie), the next comparator is used, and so on.

sortWith(users, [
  ascend(u => u.department),
  descend(u => u.age),
])

Usage

sortWith<T>(array: readonly T[], comparators: ReadonlyArray<(a: T, b: T) => number>)

Sorts an array using multiple comparators in priority order.

The first comparator has highest priority. When it returns 0 (tie), the next comparator is used, and so on.

import { sortWith } from 'massaman'
// or:  import { sortWith } from 'massaman/array'

sortWith(users, [
  ascend(u => u.department),
  descend(u => u.age),
])

Parameters

arrayreadonly T[]required

the source array.

comparatorsReadonlyArray<(a: T, b: T) => number>required

the comparators to apply in priority order.

Returns

a new array ordered by the supplied comparators. The input array is not mutated.

T[]