๐งน Data Prep ยท 7 min read
How to Sort a CSV File by Column: 4 Free Ways (2026)
Yes, you can sort a CSV by column in under a minute with nothing to install. In Google Sheets, select the data, open Data โ Sort range, and pick the column. That route is enough for most files. Excel adds multi-level sorting with its Sort dialog, the terminal handles files too big for any spreadsheet, and Python sorts files with messy quoting in full control. Two rules apply everywhere: keep the header row on top, and make sure numbers sort as numbers, not as text. Break either one and the file sorts perfectly into nonsense.
Why Bother Sorting a CSV Before Using It?
Sorted data is easier to scan, and it makes problems visible. Duplicate rows become neighbors, so a quick look down a sorted column finds repeats that hide in a shuffled file. Outliers sit at the ends. And when you merge or compare two exports, matching on sorted keys is dramatically simpler, which is why our guide to comparing CSV files assumes sorted inputs.
There is real time behind this. Surveys of data professionals keep landing in the same range: a widely cited CrowdFlower study from 2016 put the share of time spent cleaning and organizing data at about 60%, and the New York Times reported an even higher figure two years earlier. Sorting is the cheapest form of cleaning, because it costs seconds and catches the rows that would otherwise fail downstream.
Method 1: Google Sheets (Fastest for Files Under a Million Rows)
- Open the CSV in Sheets, or drag it onto sheets.new.
- Select the columns you want to sort. With a header row, select everything except it, or check the header option.
- Open Data โ Sort range โ Advanced range sorting options.
- Pick the column, choose A โ Z or Z โ A, and sort.
For repeated sorting, a formula beats the menu: =SORT(A2:C, 2, TRUE) sorts the range by column 2 ascending. The TRUE argument flips to FALSE for descending, and extra column arguments add tie-breakers, so =SORT(A2:C, 1, TRUE, 2, TRUE) sorts by column 1, then by column 2 within equal values. Google documents a spreadsheet ceiling of 10 million cells, which means a 20-column CSV tops out around 500,000 rows in one sheet.
Method 2: Excel (Multi-Column and Header Aware)
- Open the CSV in Excel and select the data range.
- Go to Data โ Sort.
- Tick My data has headers so row 1 stays put.
- Add a level for each column, in order: Sort by Region, Then by Revenue, for instance.
The dialog shows each key and its order, which handles the classic cases: sort by department first, salary second. Select the whole range before sorting, never a single column, because sorting one column alone shuffles its values against the rows next to it and destroys the record-to-record correspondence. Excel's worksheet limit is 1,048,576 rows by 16,384 columns, per Microsoft's documentation, so files beyond that need the terminal route or a split first.
Method 3: The Terminal (Any File Size, Repeatable)
macOS and Linux ship with the sort command, which sorts by column without loading the file into memory:
# sort by column 2, treating values as numbers
sort -t, -k2,2 -n sales.csv
# keep the header row on top
(head -n 1 sales.csv && tail -n +2 sales.csv | sort -t, -k2,2 -n) > sorted.csvWindows PowerShell does the same with Import-Csv sales.csv | Sort-Object Revenue -Descending | Export-Csv sorted.csv -NoTypeInformation. The header trick matters: piping the whole file through sort sinks the header into the middle of the data, which is the single most common terminal sorting mistake.
Method 4: Python (Messy Files, Full Control)
When a CSV has quoted fields with embedded commas, a spreadsheet sorts by visual column but Python's csv module parses the real structure:
import csv
with open("sales.csv", newline="", encoding="utf-8") as f:
rows = list(csv.reader(f))
header, data = rows[0], rows[1:]
data.sort(key=lambda row: float(row[1]), reverse=True) # sort by col 2, numeric
with open("sorted.csv", "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows([header] + data)The same pattern with pandas (sort_values) does it in one line if pandas is already in your stack. This route also sorts by date, by a computed field, or by any key a spreadsheet dialog cannot express.
Which Sorting Method Should You Pick?
| Method | Best when | Multi-key | Keeps header | Handles 1M+ rows | Skills needed |
|---|---|---|---|---|---|
| Google Sheets | Browser, medium files | Yes | Yes | Up to cell cap | None |
| Excel Sort dialog | Desktop, multi-level | Yes | Yes | Up to 1,048,576 rows | None |
| Terminal sort | Huge files, scripts | Yes | Manual | Yes | Command line |
| Python csv | Quoted fields, custom keys | Yes | Yes | Yes | Scripting |
Five Sorting Mistakes That Silently Corrupt Data
- Sorting one column alone. Excel and Sheets both allow it, and both shuffle that column against the rows beside it. Always select the full range.
- Numbers sorting as text. "10" sorts before "9" when values are text, which wrecks revenue and date columns. In Excel, store numbers as numbers; on the command line, add -n.
- The header sinking into the data. Piping a file through sort without isolating row 1 puts your column names in the middle. See the header trick above.
- Non-ISO dates. Sorting "2026-09-08" as text works perfectly, while "08/09/2026" sorts by day first. Write dates as YYYY-MM-DD before sorting.
- Hidden spaces. A trailing space makes "New York" sort before "Newark" unpredictably. Run the column through TRIM() in Sheets or strip() in Python first.
Cleaning before sorting also removes the duplicates that sorting is supposed to reveal. Our duplicate removal guide covers that pass, and the general cleaning guide handles mixed formats and stray delimiters.
Frequently Asked Questions
How do I sort a CSV file by column in Excel?
Select the full data range, open Data โ Sort, tick My data has headers if row 1 is a header, then choose the column and order. Add a level for a second sort key. Never sort a single column by itself, since it detaches values from their rows.
How do I sort a CSV in Google Sheets?
Select the range, open Data โ Sort range โ Advanced range sorting options, and pick the column. For a repeatable sort, use =SORT(range, column, TRUE) with TRUE for ascending or FALSE for descending.
Why does my CSV sort 10 before 9?
The column contains text, and text sorts alphabetically, so "10" precedes "9". In Excel, make sure the values are stored as numbers. In Google Sheets, wrap values with VALUE() or sort a numeric column. On the command line, add the -n flag to sort numerically.
Can I sort a CSV without Excel?
Yes. Google Sheets sorts in the browser for free. The terminal sorts any size file with sort -t, -k2,2 -n on macOS and Linux, or Import-Csv | Sort-Object in PowerShell. Python's csv module sorts files with quoted fields reliably.
How do I keep the header row on top when sorting?
Tick My data has headers in Excel's Sort dialog or use Sheets' header option. On the command line, isolate row 1 first: (head -n 1 file.csv && tail -n +2 file.csv | sort ...). Python examples sort data rows separately from the header by construction.
How do I sort a CSV by date?
Reformat dates to YYYY-MM-DD first, then sort the column as text or numbers. Ambiguous formats such as 08/09/2026 sort by the day, not the date, so normalization before sorting is not optional.
My CSV is too large for Excel to sort. What can I do?
Use the terminal or Python, which stream or hold the file without Excel's 1,048,576-row cap. If the file must end up in Excel anyway, split it into chunks, sort each chunk, and merge the results.
Tools mentioned in this guide
Want to go further with AI-powered data work? These tools pair well with NoCodeCSV:
- Stack AI โ if the same export needs sorting, deduplication, and cleanup every week, an AI workflow can run the whole pass on schedule and drop a sorted CSV where you need it. Try Stack AI
- Softr โ a sorted CSV becomes a far better data source: load it into Airtable or Sheets and Softr renders it as a searchable client directory or internal tool with the order your users expect. Try Softr
- Toggl Track โ manual re-sorting of weekly exports is exactly the kind of task that eats hours without showing up anywhere; track it once and the automation case writes itself. Try Toggl
Some links above are affiliate links โ if you buy through them we may earn a commission at no extra cost to you. Links currently point to each vendor's official partnership page until our dedicated tracking links are registered.
Clean It Before You Sort
Run your CSV through the free analyzer to flag duplicates, stray delimiters, and missing values before sorting hides them.
Related reading
File Operations โ other guides that pair well with this one.
Browse all guides in the NoCodeCSV blog.