Home / Text guides / Guide: how to sort lines

How to Sort Lines of Text

Several ways to sort lines, the differences between sort orders, and the mistakes to avoid.

Online

The quickest way is the sort lines tool. Paste your lines, choose a method and copy the result. There are dedicated pages for alphabetical, reverse, numerical and length sorting.

Linux and macOS

sort file.txt        # alphabetical
sort -r file.txt     # reverse
sort -n file.txt     # numeric
sort -u file.txt     # sort and remove duplicates
sort -f file.txt     # ignore case

The sort command depends on your locale. Running it as LC_ALL=C sort uses plain character order, which puts capital letters before lowercase ones.

Windows PowerShell

Get-Content file.txt | Sort-Object
Get-Content file.txt | Sort-Object -Descending
Get-Content file.txt | Sort-Object { [int]$_ }

VS Code and Excel

In VS Code, open the Command Palette and run Sort Lines Ascending or Sort Lines Descending. In Excel, put one item per cell in a column and use Data, then Sort A to Z.

Python

lines = text.splitlines()
sorted(lines)               # alphabetical
sorted(lines, key=len)      # by length
sorted(lines, key=float)    # numeric, if every line is a number

Common mistakes

  • Numbers sorted as text: 10 lands before 2 unless you sort numerically.
  • Case surprises: some tools put all capitals first.
  • Blank lines: empty lines sort to the top, so remove them first.
  • Trailing spaces: invisible spaces change the order. Trim lines before sorting.

Sort your own lines. Paste them in and choose alphabetical, numeric, reverse or length order.

Open the sort lines tool

Frequently asked questions

What is the difference between alphabetical and natural sort?

Alphabetical compares characters one by one, so file10 comes before file2. Natural sort reads the numbers, so file2 comes first.

How do I sort and remove duplicates at once?

Use sort -u on the command line, or tick Remove duplicates in the online tool.

Why is my sorted list in a strange order?

Common causes are numbers sorted as text, mixed case, blank lines and trailing spaces.