Skip to main content

sort

The sort command allows you to sort your data based on specific columns.

Syntax

qsv sort [options] <column> [<input_file>]

Description

The sort command sorts the rows in the input file (or stdin if no file is specified) based on the specified column(s).

Options

  • -R, --reverse: Sort in reverse order
  • -n, --numeric: Sort numerically
  • -s, --string: Sort as strings
  • -d, --date: Sort as dates
  • -m, --month: Sort as months
  • -y, --year: Sort as years
  • -t, --time: Sort as times
  • -a, --ascending: Sort in ascending order (default)
  • -D, --descending: Sort in descending order

Exit Codes

  • 0: Sort successful
  • Non-zero: An error occurred

Examples

Basic Sorting

Sort by a single column in ascending order:

qsv sort Gold olympics2024.csv | qsv select Rank,Country,Gold,Total | qsv table

Reverse Sorting

Sort by a single column in descending order:

qsv sort -R Gold olympics2024.csv | qsv select Rank,Country,Gold,Total | qsv table

Output:

Rank  Country        Gold  Total
1 United States 39 113
2 China 38 88
3 Japan 27 58
4 Great Britain 22 65
5 ROC 20 71

Sorting by Multiple Columns

Sort by multiple columns:

qsv sort -R Gold,Silver olympics2024.csv | qsv select Rank,Country,Gold,Silver,Total | qsv table

This will sort first by Gold in descending order, then by Silver for any ties in Gold medals.

Common Use Cases

  • Organizing data in a meaningful order
  • Preparing data for analysis or visualization
  • Identifying top or bottom values in a column

Tips

  • Use the appropriate sorting option based on the data type of the column
  • Combine sort with other qsv commands for more complex data manipulation

See Also

  • select - for selecting specific columns after sorting
  • table - for displaying sorted data in a tabular format