Skip to main content

sort

$sort

Returns a new sorted array. Non-mutating. Numbers are compared numerically; all other values are compared lexicographically as strings. Throws if the input is not an array.

Parameters

  • key (string|null, optional, default null): Object property key to sort by.
  • direction ('asc'|'desc', optional, default 'asc'): Sort direction.

Example

// Sort an array of numbers in ascending order
new Schema('array').transformer('$sort')
// [3, 1, 2] → [1, 2, 3]

// Sort strings in descending order
new Schema('array').transformer({$sort: {direction: 'desc'}})

// Sort an array of objects by a property
new Schema('array').transformer({$sort: {key: 'name'}})
// [{name: 'Charlie'}, {name: 'Alice'}] → [{name: 'Alice'}, {name: 'Charlie'}]

// Sort objects by a numeric property descending
new Schema('array').transformer({$sort: {key: 'score', direction: 'desc'}})