Version: v0.7.1 - Beta.  We welcome contributors & feedback.

List.sortByColumn

sortByColumn($columnKey, $options = {}) -> self

Description

Sort a multi-dimensional List (a List-of-Maps or a List-of-Lists) by values of a given column key.

// List of Maps (e.g. from `Db.selectRows`)
$users = [
    { id: 0, name: 'Bob' },
    { id: 1, name: 'Cat' },
    { id: 2, name: 'Ann' },
]

$users.sortByColumn('name')
// [
//     { id: 2, name: 'Ann' },
//     { id: 0, name: 'Bob' },
//     { id: 1, name: 'Cat' },
// ]

Example of sorting a List-of-Lists.

$points = [
    [0, 300],
    [1, 100],
    [2, 200],
]

// Sort by index 1
$points.sortByColumn(1)
// [
//     [1, 100],
//     [2, 200],
//     [0, 300],
// ]

Options

Option Value Description
reverse true/false Items will be sorted in descending order.
$users.sortByColumn('name', -reverse)
// [
//     { id: 1, name: 'Cat' },
//     { id: 0, name: 'Bob' },
//     { id: 2, name: 'Ann' },
// ]

See Also