Sort Lines

Paste a list and sort the lines alphabetically, numerically or in natural order, where server-2 comes before server-10. The same pass can trim whitespace, drop blank lines and remove duplicates, and Copy Result puts the sorted list on the clipboard.

Lines
Sorted

1 Sort Lines example

Input

1
2
3
4
5
6
7
8
server-10.internal
server-2.internal
Server-1.internal
server-2.internal

db-primary
api-gateway
server-100.internal

Natural, unique, empty lines removed

1
2
3
4
5
6
api-gateway
db-primary
Server-1.internal
server-2.internal
server-10.internal
server-100.internal

2 Which sort to pick

A to Z

Locale-aware, using Intl.Collator. Accented letters sort next to their base letter (é after e instead of after z). Digits are compared one character at a time, so 10 comes before 2.

Natural

Same collation, but runs of digits compare as numbers. Use Natural for file names, hostnames and labels with numbers in them. It gives img2, img10, img100 instead of img10, img100, img2.

Numeric

Reads the number at the start of each line, including negatives, decimals and thousands separators such as 1,000. Lines that do not start with a number go to the bottom in A to Z order.

Length, reverse, shuffle

By length counts code points, so é counts as one character. Reverse order flips the list without sorting it. Shuffle runs a Fisher-Yates shuffle fed by crypto.getRandomValues, so every order is equally likely.

3 Why your terminal disagrees

GNU sort follows your locale. With LC_ALL=C it compares raw bytes, which puts every uppercase letter before every lowercase one and pushes accented characters past z. With a UTF-8 locale it behaves much like the A to Z mode here. When a server and a laptop sort the same list differently, check the locale first. Run locale on both machines and compare LC_COLLATE.

Every sort here is stable, so lines that compare equal keep their original order. This matters with Case-insensitive on, where Apple and apple tie, and with By length, where lines of the same length keep their order relative to each other.

How to use the Sort Lines tool

  1. Paste your list into the Lines panel, one item per line, or click Upload to load a .txt, .csv or .log file. Dummy Data fills in a sample.
  2. Choose a Sort mode: A to Z, Z to A, Natural (2 before 10), Numeric, By length, Reverse order or Shuffle. Tick Descending to flip Natural, Numeric or By length.
  3. Set the options in the second row: Case-insensitive, Trim whitespace, Remove empty lines and Unique only. The Sorted panel updates as you type.
  4. Check the counts above the Sorted panel for the number of lines, and how many duplicates and empty lines were removed.
  5. Click Copy Result to put the sorted list on the clipboard. In Shuffle mode, Shuffle Again draws a fresh order.

How to sort lines in JavaScript, PHP and Python

JavaScript

const lines = ["server-10", "Server-2", "server-1", "api", "server-2", "db-primary"];

// Natural order, case-insensitive: server-1, Server-2, server-2, server-10
const natural = new Intl.Collator("en", { numeric: true, sensitivity: "base" });
console.log(lines.toSorted(natural.compare));

// Unique first, then sort (Set keeps the first copy of each line)
console.log([...new Set(lines)].sort(natural.compare));

// By length, longest first. Spread counts code points, not UTF-16 units.
console.log(lines.toSorted((a, b) => [...b].length - [...a].length));

// Numeric sort needs a comparator: the default sort compares strings
console.log(["10", "9", "100", "-1"].sort((a, b) => a - b)); // [ '-1', '9', '10', '100' ]

toSorted() returns a new array and needs Node 20 or a current browser; use [...lines].sort() on older runtimes.

PHP

<?php
$lines = ["server-10", "Server-2", "server-1", "api", "server-2", "db-primary"];

// Natural, case-insensitive. sort() reindexes; natcasesort() keeps keys.
$natural = $lines;
sort($natural, SORT_NATURAL | SORT_FLAG_CASE);
echo implode("\n", $natural), "\n";

// Unique, then sorted
$unique = array_values(array_unique($lines));
sort($unique, SORT_NATURAL); // case-sensitive: uppercase sorts first
print_r($unique);

// By length in characters, not bytes
usort($lines, fn($a, $b) => mb_strlen($a) <=> mb_strlen($b));
print_r($lines);

Python

import re
import secrets

lines = ["server-10", "Server-2", "server-1", "api", "server-2", "db-primary"]

def natural_key(s):
    # "server-10" -> ["server-", 10, ""] so the numbers compare as numbers
    return [int(t) if t.isdigit() else t.casefold() for t in re.split(r"(\d+)", s)]

print(sorted(lines, key=natural_key))
print(sorted(set(lines), key=natural_key))    # unique, then natural order
print(sorted(lines, key=len, reverse=True))   # longest first

# Shuffle with a CSPRNG instead of the default Mersenne Twister
secrets.SystemRandom().shuffle(lines)
print(lines)

Common errors and how to fix them

Numbers come out as 1, 10, 100, 2
That is string order. In the tool, pick Natural or Numeric. In JavaScript, Array.prototype.sort() compares strings unless you pass a comparator such as (a, b) => a - b. In PHP, use SORT_NATURAL or SORT_NUMERIC.
Every uppercase line sorts before every lowercase one
That is code point order, which LC_ALL=C sort, PHP sort() with default flags and Python sorted() all use. Use a collator (Intl.Collator or a locale-aware sort), SORT_FLAG_CASE in PHP, or key=str.casefold in Python.
A blank line appears at the top of the result
Empty lines sort first. Leave Remove empty lines ticked, and in code filter out lines where line.trim() is empty after splitting.
Unique only leaves lines that look identical
They differ in something invisible, usually trailing whitespace or a Windows \r. Tick Trim whitespace. In code, split on /\r?\n/ rather than "\n" and trim before comparing.
Sorting by length gives odd results for accented text or emoji
PHP strlen() counts bytes and JavaScript .length counts UTF-16 code units, so é or an emoji counts as more than one. Use mb_strlen() in PHP or [...str].length in JavaScript to count code points.

Good to know

Since ES2019, the spec requires Array.prototype.sort() to be stable, so lines that compare equal keep their original relative order. Python's sorted() and PHP 8's sort() are stable too. PHP 7 and older were not, so do not rely on tie order there.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Frequently Asked Questions

How do I sort lines alphabetically?

Paste the list, one item per line, and pick A to Z or Z to A in the Sort dropdown. The comparison is locale-aware (it uses Intl.Collator), so accented letters sort next to their base letter instead of after z.

Why does 10 come before 2 when I sort?

Alphabetical sorting compares one character at a time, and the character 1 comes before 2. Pick Natural to compare runs of digits as numbers (file2 before file10), or Numeric to sort by the number at the start of each line.

Is the sort case-sensitive?

Case only breaks ties by default: apple and Apple sit together, with the uppercase one first. Tick Case-insensitive to treat them as equal, which keeps their original order and makes Unique only treat them as duplicates.

How random is the shuffle?

It is a Fisher-Yates shuffle driven by crypto.getRandomValues with rejection sampling, so every ordering is equally likely. Shuffle Again draws a new order.

Is there a limit on the number of lines?

No fixed limit. The list is sorted in your browser tab, so the practical limit is your device's memory, and nothing is uploaded. For multi-gigabyte files, GNU sort on the command line is the better choice.

More line tools

To drop repeats without reordering, use remove duplicate lines. The diff checker compares two sorted lists, the case converter normalizes the entries first, and the word counter tells you how long the list got.

More Text & Content Tools

Browse all text & content tools

Copied to clipboard!