Skip to main content

Sorting and Key Columns

Sorting in VisiData is instant — even on million-row datasets — because it operates on the in-memory sheet view. Key columns are a special designation that affects sorting, joins, and pivot tables.

Learning Focus

Understand the difference between single-column sort ([/]) and key-column multi-sort (g[/g]), and why setting key columns before pivoting or joining is critical.

Single Column Sort

[ sort ascending by current column
] sort descending by current column

Example:

# Move cursor to 'salary' column
[sort ascending (lowest first)
]sort descending (highest first)
warning

Without typing a numeric column with # or %, sort is lexicographic. 10 < 9 in string sort. Always type columns before sorting numbers.

Multi-Column Sort (Key Columns)

Key columns define a multi-level sort hierarchy. Mark multiple key columns, then sort by all of them:

! toggle current column as key column (blue highlight)
z! unset current column as key column

g[ sort ascending by ALL key columns
g] sort descending by ALL key columns
gz[ sort ascending by current column, keep existing sort
gz] sort descending by current column, keep existing sort

Example — sort by region then by salary:

# Move to 'region' column, press ! → key column 1
# Move to 'salary' column, press ! → key column 2
# Press # → cast salary to int
g[sort ascending by region, then salary

Key Columns for Joins

Key columns are the join keys when merging two sheets with &:

Key Columns for Pivot Tables

Key columns define the row groups in a pivot table (Shift+W):

# Set 'region' as key column
!
# Press Shift+W
# → Pivot table: rows = region values, columns = aggregated data

Moving Rows

J slide current row down
K slide current row up
gJ slide current row to bottom
gK slide current row to top

Stability of Sort

VisiData's sort is stable — rows with equal values in the sort column retain their relative order from the previous sort. This means you can sort by column B then by column A to get a multi-level sort without marking key columns.

Practical Use Cases

Sort Servers by Region and Role

vd /tmp/servers.csv

# Mark key columns
# Move to 'region', press !
# Move to 'role', press !

# Sort ascending
g[
# Servers now grouped by region, then by role within each region

Find the Top 10 Largest Log Entries

vd /var/log/nginx/access.log

# Move to 'bytes_sent' column
# Cast to int: press #

# Sort descending
]
# Top rows are now the largest responses

Sort Products by Price Tier

vd /var/www/html/exports/products.csv

# Move to 'price', press % → float cast
# Sort descending
]
# Highest-priced products at top

Sort Order Reference

KeysEffect
[Sort ascending by current column (replaces existing sort)
]Sort descending by current column
g[Sort ascending by all key columns
g]Sort descending by all key columns
z[Sort ascending by current column (add to existing)
z]Sort descending by current column (add to existing)
gz[Sort ascending by all key columns (add to existing)
gz]Sort descending by all key columns (add to existing)

Troubleshooting Matrix

ProblemCauseFix
Numbers sort wrong (1,10,2,20)Column is string typeCast to # first
Sort ignores key columnsUsed [ instead of g[Use g[ for multi-column sort
Sort resets after reloadSheet reloaded from sourceRe-apply sort after ^R
Key column not highlightedNot set correctlyPress ! on correct column

Hands-On Practice

vd /tmp/servers.csv

# 1. Move to 'role', press ! (key col 1)
# 2. Move to 'status', press ! (key col 2)
# 3. Press g[ → sorted by role then status
# 4. Press ] on 'hostname' column → sort by hostname descending
# 5. Press g! (on Columns Sheet) to unset all key columns

What's Next