Sorts a list of items and returns a new array.
✅
Immutable: This does not mutate the given array.
The following types are supported:
- Number
- String
- Boolean
- Date
⚠️
It cannot sort if the values in the array is of mixed types.
Related:
Syntax
sort<T>(arr: T[], order = 'asc'): T[]
The order param can be either 'asc'
or 'desc'
.
Usage
import { sort } from "@opentf/std";
sort([...]);
Examples
sort([1, 3, 2]) //=> [1, 2, 3]
sort(['x', 'z', 'y'], 'desc') //=> ['z', 'y', 'x']