Version: v0.5.1 - Beta.  We welcome contributors & feedback.  THanks!

List.sortTable

sortTable(key, descendingOrder = false)

Description

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

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

table.sortTable('name');
// [
//     { id: 2, name: 'Ann' },
//     { id: 0, name: 'Bob' },
//     { id: 1, name: 'Cat' },
// ];


// Descending order
table.sortTable('name', true);
// [
//     { id: 1, name: 'Cat' },
//     { id: 0, name: 'Bob' },
//     { id: 2, name: 'Ann' },
// ];

// List of Lists
let table = [
    [0, 300],
    [1, 100],
    [2, 200]
];

// Sort by index 1
table.sort(1);
// [
//     [1, 100],
//     [2, 200],
//     [0, 300],
// ];

See Also