Skip to main content

Sort and Filter

Sort

This is an optional param. Pass the sort function that will be used on each row to sort them. This sort function is simple Array element sort function

const { Table } = require("console-table-printer");

const p = new Table({
columns: [{ name: "index" }, { name: "text" }, { name: "value" }],
sort: (row1, row2) => +row2.value - +row1.value, // desc sorting order of rows (optional),
});

p.addRow({ index: 1, text: "red wine", value: 11 }, { color: "green" });
p.addRow({ index: 2, text: "green gemuse", value: 21 });
p.addRow({ index: 3, text: "gelb bananen", value: 10 });
p.addRow({ index: 3, text: "rosa hemd wie immer", value: 13 });

p.addRow({ index: 4, text: "some more shit", value: 20 }, { color: "cyan" });

p.printTable();
Screenshot

Filter

This is an optional param. Pass the filter function that will be used on each row to filter them

const { Table } = require("console-table-printer");

const p = new Table({
columns: [{ name: "index" }, { name: "text" }, { name: "value" }],
filter: (row) => +row.value < 20, // filter rows with value < 20
});

p.addRow({ index: 1, text: "red wine", value: 11 }, { color: "green" });
p.addRow({ index: 2, text: "green gemuse", value: 21 });
p.addRow({ index: 3, text: "gelb bananen", value: 10 });
p.addRow({ index: 3, text: "rosa hemd wie immer", value: 13 });

p.addRow({ index: 4, text: "some more shit", value: 20 }, { color: "cyan" });

p.printTable();
Screenshot