csvsort
Sort CSV (Comma Separated Values) files
TLDR
Sort a CSV file by column 9
Sort a CSV file by the "name" column in descending order
Sort a CSV file by column 2, then by column 4
Sort a CSV file without inferring data types
SYNOPSIS
csvsort [options] [FILE]
PARAMETERS
-c COLUMNS, --columns COLUMNS
Columns to sort by (repeatable). Use names or 0-based indices; prefix with - for descending.
-r, --reverse
Reverse (descending) sort order for all columns.
-H, --no-header-row
Input lacks header row; use indices only.
--basic
Use basic quoting (minimal escaping).
--delimiter DELIM
Field delimiter (default: ,)
--doublequote
Double quotes instead of escape characters.
--escapechar ESCAPE_CHAR
Escape character (default: ")
--line-terminator LINE_TERMINATOR
Line ending (default: \n)
--maxfieldsize MAXFIELDSIZE
Max field length (default: 1048576 bytes).
--quotechar QUOTECHAR
Quote character (default: ")
--quoting QUOTING
Quoting style: 0=none, 1=minimal, 2=all, 3=nonnumeric (default: 1).
--skipinitialspace
Ignore spaces after delimiter.
DESCRIPTION
csvsort is a powerful command-line tool from the csvkit suite for sorting rows in CSV files by one or more columns. It preserves CSV structure, handling quoting, delimiters, and headers intelligently. By default, it treats the first row as a header, excluding it from sorting while using column names for reference.
Specify columns using names (if header present) or zero-based indices. Multiple columns enable multi-level sorts; prefix with - for descending order on that column only (e.g., -c date,-amount sorts date ascending, amount descending). It auto-detects data types like numbers, dates for proper sorting.
Reads from stdin if no file given, ideal for pipelines. Supports custom dialects via options for delimiters, quoting, escapes. Useful for data prep in analysis workflows, before tools like csvstat or csvcut.
Sorting uses Python's stable sorted(), case-sensitive by default. Loads files into memory, so large datasets may require ample RAM.
CAVEATS
Loads entire file into memory; unsuitable for huge datasets. Case-sensitive sorting. No built-in stable sort guarantee beyond Python's behavior.
EXAMPLES
csvsort -c lastname,firstname data.csv
csvsort -c date,-revenue sales.csv
csvcut -c id,name sales.csv | csvsort -c name | csvlook
COLUMN SPECIFIERS
Names: colname (header required).
Indices: 2 (0-based).
Descending: -2.
HISTORY
Introduced in csvkit 0.9.0 (2011) by Christopher Groskopf; evolved with Python CSV module improvements. Maintained by Kenneth Reitz and community.


