Unit 04.00: Sorting is the analysis
For "which is largest?", sorting is not presentation - it is the analysis.
Alphabetical answers a question nobody asked
Five categories in the order the data arrived, and in the order the question implies.
The code shows both.
DATA = {"North": 412, "South": 388, "East": 455, "West": 371, "Central": 402}
print("alphabetical, as the data arrived:")
for name in sorted(DATA):
print(f" {name:9} {DATA[name]:>4}")
print("\nsorted by value, as the question implies:")
for name, value in sorted(DATA.items(), key=lambda kv: -kv[1]):
print(f" {name:9} {value:>4}")
top, second = sorted(DATA.values(), reverse=True)[:2]
print(f"\nsorted, the answer is immediate: East leads by {top - second} "
f"({(top - second) / second:.1%})")
# For "which is largest?", sorting IS the analysis. Alphabetical order answers
# "where is Central?", which nobody asked. Sort by the value unless the
# categories have a natural order -- days, sizes, stages.
Sorted by value, the answer is immediate and so is the margin. Alphabetically, the reader has to scan all five and hold them in mind - which answers "where is Central?", a question nobody asked.
The exception is categories with a natural order: days of the week, size bands, pipeline stages. Sorting those by value destroys information the reader was using.
The mistake this prevents
The mistake is leaving the order the data arrived in, because it feels neutral. It is not neutral - it is alphabetical, or database order, and both actively obstruct the comparison the chart exists for.
Takeaway
Sort by the value the question asks about, unless the categories have a natural order. Arrival order is not neutral.
