This commit is contained in:
@ -147,6 +147,27 @@ void tableprint::set_title(const std::string title) {
|
||||
this->title = title;
|
||||
}
|
||||
|
||||
// gives the columns to sort by, starting at 0.
|
||||
void tableprint::sort(std::vector<int> sort_columns)
|
||||
{
|
||||
// Skip header row and sort remaining rows
|
||||
if (rows.size() <= 1) return; // Only header or empty table
|
||||
|
||||
// Create a custom comparator that compares rows based on the specified columns
|
||||
auto comparator = [this, &sort_columns](const std::vector<std::string>& a, const std::vector<std::string>& b) {
|
||||
for (int col : sort_columns) {
|
||||
if (col >= 0 && col < a.size() && col < b.size()) {
|
||||
int cmp = a[col].compare(b[col]);
|
||||
if (cmp != 0) return cmp < 0;
|
||||
}
|
||||
}
|
||||
return false; // Equal rows maintain original order
|
||||
};
|
||||
|
||||
// Sort rows starting from index 1 (after header)
|
||||
std::sort(rows.begin() + 1, rows.end(), comparator);
|
||||
}
|
||||
|
||||
void tableprint::add_row(const std::vector<std::string>& row) {
|
||||
std::vector<std::string> trimmed_row;
|
||||
for (const auto& cell : row) {
|
||||
|
Reference in New Issue
Block a user