Robpol86 / terminaltables

Project no longer maintained.
https://github.com/matthewdeanmartin/terminaltables
MIT License
689 stars 70 forks source link

Feature Request: Sort table by column key. #13

Closed iCHAIT closed 8 years ago

iCHAIT commented 8 years ago

It would be nice if the table could be sorted according to any of the column keys.

Example -

Column1 Column 2
22 37
11 50
33 10

Something like this-

table = AsciiTable(table_data, sortby="Column1")

Which would generate this -

Column1 Column 2
11 50
22 37
33 10
Robpol86 commented 8 years ago

I think this is out of scope for terminaltables. This is something relatively easy to do in Python:

>>> from terminaltables import AsciiTable
>>> table = [['Column1', 'Column2'], ['22', '37'], ['11', '50'], ['33', '10']]
>>> print(AsciiTable(table).table)
+---------+---------+
| Column1 | Column2 |
+---------+---------+
| 22      | 37      |
| 11      | 50      |
| 33      | 10      |
+---------+---------+
>>> print(AsciiTable(table[:1] + sorted(table[1:], key=lambda i: i[0])).table)
+---------+---------+
| Column1 | Column2 |
+---------+---------+
| 11      | 50      |
| 22      | 37      |
| 33      | 10      |
+---------+---------+
>>> 

Things that belong in terminaltables are those that can't be done outside of it, like column justification or enabling/disabling borders. I don't really see the value in adding sorting code here.