Sorting Tables with Multiple Sections
psv takes special care to not destroy your tables.
Take this simple table for example:
+-----------+------------+-------------+--------+
| Animal | Color | Avg. Weight | |
| --------- | ---------- | --------.-- | ------ |
| elephant | grey | 5_000 kg | skin |
| giraffe | patchy | 1_000 kg | skin |
| --------- | ---------- | ----------- | ------ |
| lion | | 100 kg | fur |
| crocodile | grey | | scales |
| leopard | camouflage | 50 kg | fur |
+-----------+------------+-------------+--------+
If you run the sample table in this section through the unix
sort(1) program, you will typically get something like this:
+-----------+------------+-------------+--------+
+-----------+------------+-------------+--------+
| Animal | Color | Avg. Weight | |
| --------- | ---------- | ----------- | ------ |
| --------- | ---------- | --------.-- | ------ |
| crocodile | grey | | scales |
| elephant | grey | 5_000 kg | skin |
| giraffe | patchy | 1_000 kg | skin |
| leopard | camouflage | 50 kg | fur |
| lion | | 100 kg | fur |
The problem here is that the table has multiple sections, separated by
horizontal rulers.
sort obviously has no way to know this.
psv, however, only sorts the rows between
horizontal rulers.
Sort our animals by weight:
+-----------+------------+-------------+--------+
| Animal | Color | Avg. Weight | |
| --------- | ---------- | --------.-- | ------ |
| elephant | grey | 5_000 kg | skin |
| giraffe | patchy | 1_000 kg | skin |
| --------- | ---------- | ----------- | ------ |
| lion | | 100 kg | fur |
| crocodile | grey | | scales |
| leopard | camouflage | 50 kg | fur |
+-----------+------------+-------------+--------+
Tables can be re-sorted by simply re-formatting them if they have sorting hints
in their rulers.
+-----------+------------+-------------+--------+
| Animal | Color | Avg. Weight | |
| --------- | ---------- | --------.-^ | ------ |
| elephant | grey | 5_000 kg | skin |
| giraffe | patchy | 1_000 kg | skin |
| --------- | ---------- | ----------- | ------ |
| lion | | 100 kg | fur |
| crocodile | grey | | scales |
| leopard | camouflage | 50 kg | fur |
+-----------+------------+-------------+--------+
This has the added side effect that a column header will also remain at the top
of the table without requiring any special exceptions.