Skip to main content

Sorting and Reordering

Sorting in VisiData is instant — even on million-row datasets — because it operates on the in-memory sheet view. You can also manually reorder rows by moving them, randomize the order, or reverse it entirely.

Learning Focus

Use [ / ] for quick single-column sort. Set key columns with ! then press g[ for multi-column sort. Use J / K to manually move rows into position.

Sample Data

hostname role status region score
web01 webserver active sg 90
web02 webserver standby sg 70
db01 database active kl 85
cache01 cache active sg 60
monitor01 monitoring standby jk 50

Sort Ascending by One Column

Move cursor to the score column, press [:

hostname role status region score
monitor01 monitoring standby jk 50 ← lowest
cache01 cache active sg 60
web02 webserver standby sg 70
db01 database active kl 85
web01 webserver active sg 90 ← highest
# Move cursor to 'score' column
[ # sort ascending (lowest first)
] # sort descending (highest first)
warning

Without casting a numeric column to # (int) or % (float), sort is lexicographic100 sorts before 9 because 1 < 9. Always type-cast numeric columns before sorting.

See Column Types and Casting for details.

Multi-Column Sort with Key Columns

Key columns define a multi-level sort hierarchy. Mark multiple columns with !, then sort all of them at once with g[.

Step 1: Mark region as key column 1:

# Move to 'region', press !
# 'region' header turns blue

Step 2: Mark score as key column 2:

# Move to 'score', press !
# 'score' header turns blue

Step 3: Sort by all key columns:

g[ # sort ascending: first by region, then by score within region

Result — grouped by region, then sorted by score within each region:

hostname role status region score
monitor01 monitoring standby jk 50 ← jk region
db01 database active kl 85 ← kl region
cache01 cache active sg 60 ← sg region (lowest score)
web02 webserver standby sg 70 ← sg region
web01 webserver active sg 90 ← sg region (highest score)

Key Column Commands

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

g[ sort ascending by ALL key columns
g] sort descending by ALL key columns

For key columns in joins and pivot tables, see:

Additive Sort

Add additional sort criteria without replacing the existing sort order.

z[ sort ascending by current column (keep existing sort)
z] sort descending by current column (keep existing sort)
gz[ sort ascending by all key columns (keep existing sort)
gz] sort descending by all key columns (keep existing sort)

Example: Sort by region first, then add score within each region:

# First sort by region
# Move to 'region' → press [
[ # primary sort: region

# Then add score sort within each region
# Move to 'score' → press z[
z[ # secondary sort: score (additive)

Move Rows Manually

Manually reposition rows in the sheet.

J slide current row one position down
K slide current row one position up
gJ slide current row to bottom of sheet
gK slide current row to top of sheet

Example: promote a row to the top:

# Move cursor to the row you want at the top
gK # sends it to the top

# Or move it down 3 positions
J J J

Randomize and Reverse

zR randomize the order of all (or selected) rows
gzR reverse the current row order

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)

Moving Reference

KeysEffect
JMove row down one position
KMove row up one position
gJMove row to bottom
gKMove row to top
zRRandomize row order
gzRReverse row order

Troubleshooting

ProblemCauseFix
Numbers sort wrong (1, 10, 2, 20)Column is string typeCast to # or % first
Sort ignores key columnsUsed [ instead of g[Use g[ for multi-column sort
Sort resets after reloadSheet reloaded from sourceRe-apply sort after Ctrl+R

What's Next