ConvertCSV

How to Sort CSV Files by Column (Online or with Code)

By Convert CSV Editorial TeamLast updated August 1, 2026

Sort CSV files by one or more columns. Handle numbers, dates, and text correctly, then export a clean, stable, sortable file.

Why Sort a CSV File?

Sorting turns a raw dump into something a human can scan. It also enables efficient joins, deduplication, and reproducible reports.

  • Order transactions by date for a monthly reconciliation.
  • Group customers by country before an outreach campaign.
  • Line up rows by ID before diffing two exports in git.
  • Prepare data for pagination or top-N reporting.

Text vs Number vs Date Sorting

Sorting order depends on the column type. Treating everything as text produces wrong results.

Column typeWhat to watch for
TextCase sensitivity, accents, locale collation
NumbersDo not sort as text ("10" would come before "2")
DatesUse ISO 8601 (YYYY-MM-DD) or parse to real date types
MixedSplit into typed columns before sorting

Step-by-Step: Sort CSV Correctly

Sort is a two-line command—but the quality of the input dictates the quality of the sort.

  • Preview the CSV in the Online CSV Editor.
  • Confirm the column type you plan to sort on.
  • Trim whitespace and normalize case if needed.
  • Sort by primary key, then secondary (e.g., date DESC, then id ASC).
  • Verify the first and last rows look right.
  • Download the sorted CSV.

Python: sort by two columns

Read, sort, write.

import csv

with open("orders.csv", newline="", encoding="utf-8") as f:
    rows = list(csv.DictReader(f))

rows.sort(key=lambda r: (r["region"], int(r["amount"])), reverse=False)

with open("orders_sorted.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=rows[0].keys())
    writer.writeheader()
    writer.writerows(rows)

Node.js: numeric-aware sort

Cast to numbers to avoid text sort pitfalls.

rows.sort((a, b) => {
  if (a.region !== b.region) return a.region.localeCompare(b.region);
  return Number(a.amount) - Number(b.amount);
});

Sorting in Spreadsheet Apps

Excel, Google Sheets, and LibreOffice have built-in sort UIs. They are convenient—and easy to misuse.

  • Always include the header row in the sort selection so headers stay put.
  • Use custom sort with multiple levels for compound sorts.
  • Confirm number vs text detection on the sort column.

Stable vs Unstable Sort

Stable sorts preserve the original relative order of equal-key rows. That matters when your compound sort depends on the previous order.

Language / toolSort behavior
Python sorted()Stable
JavaScript Array.sort()Stable in modern engines
Common SQL ORDER BYNot guaranteed stable without a tiebreaker

Real-World Examples

Sort is quiet infrastructure that many downstream jobs depend on.

Finance monthly close

Sort transactions by date, then by account, so reconciliation reads like a story instead of chaos.

Ecommerce inventory

Sort by warehouse, then SKU. The picker flow becomes predictable.

Class gradebook

Sort by student_id (ASC), then submission timestamp (DESC), then keep the latest.

Common Mistakes

Sort bugs often hide until someone builds a report on top.

  • Sorting numbers as text (10 before 2).
  • Sorting mixed date formats.
  • Excluding the header from the sort selection and destroying it.
  • Losing ties when a stable sort was assumed but not guaranteed.
  • Sorting only part of the sheet, misaligning columns.

Best Practices

Sort should be boring and repeatable.

  • Use ISO dates before sorting by date.
  • Cast to numbers before sorting by amount.
  • Prefer stable sorts for reproducibility.
  • Add a secondary sort key to break ties deterministically.
  • Verify first/last rows after every sort.

Why Use Convert CSV Online?

Convert CSV Online is free, browser-based, and account-free for everyday conversions. Use the Online CSV Editor to preview, tidy, and verify sorted output—then export to Excel, JSON, or SQL as needed. Client-side workflows run on Windows, macOS, and Linux browsers.

Preview sorted CSV now

Open the file online, verify first and last rows, and download a clean sorted CSV ready for downstream systems.

Conclusion

A good sort is really a good schema. Type-aware sorting on clean columns is fast, stable, and repeatable.

FAQ

How do I sort a CSV file?

Choose the sort column(s), confirm the column type, sort with a script or spreadsheet, and verify the result before download.

Why is 10 sorted before 2 in my CSV?

That column is being sorted as text. Cast the values to numbers before sorting.

How do I sort CSV by date?

Use ISO 8601 (YYYY-MM-DD) dates and sort as text, or parse to date objects. Avoid mixed date formats.

Can I sort by multiple columns?

Yes. Sort by primary column first, then by secondary. Stable sort algorithms preserve ties from the previous pass.

Does Excel sort include the header row?

Only if you tell it to. Use My data has headers to keep the header row in place.

Can I sort CSV online?

For moderate files, an Online CSV Editor is enough. For very large data, use a script or database.

References

Convert your CSV in the browser

Preview, clean, and convert CSV files free with Convert CSV Online—no installation and no account required for everyday conversions.