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
- Click any cell in the data.
- Data → Sort.
- Tick My data has headers, so the header row stays at the top.
- Sort by: the column. Order: A to Z, smallest to largest, or oldest to newest, depending on what Excel thinks the column holds.
- 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 (-nin Miller), or use DuckDB, which detects the type. - Currency and thousands separators.
$1,200is text to almost everything. Strip the symbol and the comma first. - Dates in day/month order.
03/04/2026sorts by the first number. If the column isn't recognised as dates, rewrite it as2026-04-03, which sorts correctly even as text. - Case and spaces.
apple,AppleandApple(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.

Sort by up to three columns on iPhone or Android, by value rather than as text, without changing the file.
What it doesTo 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.
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 doesCheck the result
Thirty seconds of checking catches every problem above:
- Look at the top and bottom few rows of the sorted column. Is the largest value where you expect?
- Pick one row and check it's still whole. Find a record you know and confirm its name, email and amount still belong together.
- Count the rows. A sort should never add or remove one.