mygems

How to sort a CSV file without scrambling it

Sort one column alone and every row is scrambled; sort numbers as text and 100 comes before 9. Here is how to sort a CSV safely, by one column or several.

By mygems ·

Sorting a CSV goes wrong in two ways, and neither one shows an error. Sort a single column without the others and every row's data is scrambled. Sort numbers that the tool thinks are text and 100 lands before 9. Both look like a sorted file. This guide covers sorting properly in a spreadsheet, on the command line and on a phone, and how to check you got what you meant.

The fast answer

  • Excel: click inside the data, Data → Sort, tick My data has headers, choose the column. Never select one column and press sort.
  • Google Sheets: Data → Sort range → Advanced range sorting options, tick Data has header row.
  • Command line: csvsort -c price data.csv, or Miller, mlr --csv sort -nr price data.csv.
  • Several columns (country, then date): every tool above does it; the syntax is below.
  • Numbers sorting as 1, 10, 100, 2: the column is text. Fix the type, then sort.

The mistake that scrambles a file

In Excel, if you select a single column and click Sort A to Z, Excel asks whether to expand the selection. Say no, and it sorts that column on its own: the names are now in alphabetical order, and each one sits next to someone else's email address and balance. If the file is saved, there's no getting the rows back.

The fix is to never start from a column selection. Click any single cell in the data and use Data → Sort; Excel finds the whole table itself. And keep an untouched copy of the original until you've checked the result.

In Excel

  1. Click any cell in the data.
  2. Data → Sort.
  3. Tick My data has headers, so the header row stays at the top.
  4. Sort by: the column. Order: A to Z, smallest to largest, or oldest to newest, depending on what Excel thinks the column holds.
  5. Add Level for a second column, used to break ties in the first.

Because this is a CSV, check what Excel did to the data before it sorted anything. A double-clicked CSV has already had its leading zeros stripped and some values turned into dates. Import it instead if the file has IDs, postcodes or codes like 3-4.

In Google Sheets

Data → Sort range → Advanced range sorting options, tick Data has header row, and pick columns. Add another sort column works like Excel's Add Level.

Or with a formula, which leaves the original untouched and writes a sorted copy:

=SORT(A2:F, 3, FALSE, 1, TRUE)

That sorts by the third column descending, then the first ascending. Put it in a new tab and download that tab as CSV.

On the command line

sort is the wrong tool. sort -t, -k3 splits on every comma, including commas inside quoted fields, and sorts the header into the data. Use a CSV-aware tool.

csvkit:

csvsort -c price data.csv > sorted.csv
csvsort -c country,date -r data.csv > sorted.csv

Miller, where the flag says how to compare (-f text ascending, -nr numeric descending, -t natural text):

mlr --csv sort -f country -nr revenue data.csv > sorted.csv

DuckDB, for very large files, or when the sort is part of a bigger question:

duckdb -c "COPY (SELECT * FROM 'data.csv' ORDER BY country, revenue DESC) TO 'sorted.csv'"

Why the order looks wrong

Every one of these produces a file that looks sorted until you read it.

  • Numbers stored as text. Text sorts character by character, so 100, 20, 3. In a spreadsheet, a green triangle in the corner of the cell gives it away. On the command line, tell the tool it's numeric (-n in Miller), or use DuckDB, which detects the type.
  • Currency and thousands separators. $1,200 is text to almost everything. Strip the symbol and the comma first.
  • Dates in day/month order. 03/04/2026 sorts by the first number. If the column isn't recognised as dates, rewrite it as 2026-04-03, which sorts correctly even as text.
  • Case and spaces. apple, Apple and Apple (leading space) don't sort together in every tool. Trim and normalise first.
  • Blanks. Some tools put empty cells first, others last. Decide which you want.

Sorted for reading, or sorted in the file?

Two different jobs, again. Sometimes you only want to read the file in order: the biggest transactions first, or everyone in one country together. Then the file shouldn't change at all, and a viewer that sorts the display is safer than an editor that rewrites the rows.

Other times the sorted order is the point: the file is going to someone who expects it ranked. Then you need the rows written back in that order.

On a phone, those are different apps. In Smart CSV Viewer, holding a column name sorts by it, and the Sort panel sorts by up to three columns, each ascending or descending. A number or date column sorts by value, not as text. The file itself is never reordered.

The Sort panel sorting by Continent ascending, then by Rating descending, over a table of trips
Two levels: Continent A to Z, then Rating highest first
CSV File Viewer - Smart CSViPhone & iPad · Android

Sort by up to three columns on iPhone or Android, by value rather than as text, without changing the file.

What it does

To change the file's order, CSV Editor sorts the rows themselves: long-press a column header and choose ascending or descending, worded by the column's type (Smallest first, A→Z). The header row stays pinned, the sort can be undone, and the result is saved back into the CSV.

CSV Editor - Smart CSViPhone & iPad

Sorts the rows in the file itself on iPhone and iPad, with the header kept in place and Undo if it wasn't what you meant.

What it does

Check the result

Thirty seconds of checking catches every problem above:

  1. Look at the top and bottom few rows of the sorted column. Is the largest value where you expect?
  2. Pick one row and check it's still whole. Find a record you know and confirm its name, email and amount still belong together.
  3. Count the rows. A sort should never add or remove one.

Questions

How do I sort a CSV file by a column?
In Excel, click a cell in the data, choose Data → Sort, tick My data has headers and pick the column. On the command line use csvsort -c column file.csv or Miller's sort verb. Never sort a single selected column on its own.
Why does my CSV sort 1, 10, 100, 2 instead of numerically?
The column is being treated as text, which sorts character by character. Convert the column to numbers, remove currency symbols and thousands separators, or tell the tool to sort numerically.
How do I sort a CSV by multiple columns?
In Excel use Data → Sort and Add Level; in Google Sheets use Add another sort column. On the command line, csvsort -c country,date or mlr --csv sort -f country -nr revenue sort by several columns in order.
Can I sort a CSV with the sort command?
Not safely. The Unix sort command splits on every comma, including those inside quoted fields, and sorts the header into the data. Use a CSV-aware tool such as csvkit, Miller or DuckDB.
How do I sort a CSV file without Excel?
Google Sheets sorts CSVs for free, csvkit, Miller and DuckDB sort them on the command line at any size, and CSV apps on phones sort them directly, either for viewing or by rewriting the file.

mygems — Builds CSV Editor and Smart CSV Viewer, two apps that exist because spreadsheets give up on big files.