For the complete documentation index, see llms.txt. This page is also available as Markdown.

sortBy

Sorts the input array by the given expression, either ascending or descending.

Defaults to ascending.

Examples

Simple array

[1, 3, 5, 2, 4].sortBy() // [1, 2, 3, 4, 5]
[1, 3, 5, 2, 4].sortBy($this) // [1, 2, 3, 4, 5]
[1, 3, 5, 2, 4].sortBy($this, asc) // [1, 2, 3, 4, 5]
[1, 3, 5, 2, 4].sortBy($this, desc) // [5, 4, 3, 2, 1]

Reverse original order using $key

[10, 20, 30].sortBy($key, desc) // [30, 20, 10]

Sort by nested property

[
    {"x": 1},
    {"x": 3},
    {"x": 5},
    {"x": 2},
    {"x": 4}
].sortBy(x)

// Results in
[
    {"x": 1},
    {"x": 2},
    {"x": 3},
    {"x": 4},
    {"x": 5}
]

Last updated