Sort Lines

Sort lines alphabetically, by length, numerically, or randomly. Reverse order supported.

Sorted in your browser — your text is never uploaded.
1 lines • 0 chars
Sort Options
Runs entirely in your browser. No uploads. Your files stay private.

How Sort Lines Orders Text

Sort Lines splits the input on newline characters, applies a comparator chosen from the eight sort modes (alphabetical, length, numeric, word-count, natural, random, reverse, column), and joins the result back with newlines. The underlying engine is JavaScript's built-in Array.prototype.sort, which the V8 and SpiderMonkey teams implement as a stable Timsort variant. Since the 2019 ECMAScript spec, sort stability is guaranteed across browsers, so equal keys keep their original relative order.
Alphabetical mode uses String.prototype.localeCompare, which respects the user's locale collation rules: in English the order matches dictionary expectations (a before A in case-sensitive mode, both equivalent in case-insensitive mode), in German the eszett ß sorts near 'ss', and in Swedish ä sorts after z. This is more correct than a naive Unicode code-point comparison, which would put 'Z' (U+005A) before 'a' (U+0061) and break for any non-ASCII text.
Natural sort handles strings that mix letters and digits — 'file2' before 'file10' — by chunking each line into runs of digits and runs of non-digits, then comparing chunk-by-chunk. Numeric chunks are compared as numbers, alphabetic chunks via localeCompare. This matches Windows Explorer's file ordering and is the only correct option for version strings, paginated filenames, and ticket numbers.
Numeric mode extracts the first number it can parse from each line (matching the regex /-?\d+(?:\.\d+)?/) and sorts on that. Lines with no parseable number sort as zero and clump together. This is useful for sorting log lines by timestamp prefix or CSV rows by a specific column once you have isolated it. For full numeric precision use the column mode with a delimiter that matches your data.
Column mode splits each line on a chosen delimiter (tab, comma, pipe, or custom), takes the nth field, and uses that as the sort key. The remaining fields ride along — only the sort key is extracted, not the line contents. This is the right tool for sorting CSV-like text by a non-first column without round-tripping through a spreadsheet.
Random mode uses the Fisher-Yates shuffle backed by Math.random(). For small lists this is fine. If you need cryptographically random shuffling (for raffles or unbiased trial assignment) the underlying Math.random is not suitable — use crypto.getRandomValues in a small script instead. The shuffle is in-place on a copy, so the original input is preserved.
Empty-line handling is independently configurable: keep, remove, push to top, or push to bottom. This matters when sorting blocks of text where blank lines are paragraph separators. The deduplicate option runs after sorting and removes adjacent equal lines, which is a one-line dedupe given the input is already sorted (Set-based dedupe is faster but does not require sorting). Sorting plus dedupe matches the behavior of Unix `sort -u`.

Common Use Cases

01

Filename ordering

Use natural sort so 'screenshot-2.png' comes before 'screenshot-10.png' instead of after, matching Finder and Explorer behavior.

02

Version list sorting

Sort semver tags like v1.10.0 and v1.9.0 with natural sort so 1.10 comes after 1.9 rather than before.

03

CSV column sort

Pick a delimiter and column index to sort comma- or tab-separated rows by a non-first field without opening a spreadsheet.

04

Log line ordering

Sort log dump lines numerically by timestamp prefix or alphabetically by severity to consolidate scattered entries.

Frequently Asked Questions

Natural sort treats runs of digits as numbers. 'file2' has a numeric chunk of 2, 'file10' has a numeric chunk of 10, so file2 sorts first. Alphabetical sort compares character-by-character — '1' (U+0031) comes before '2' (U+0032), so file10 sorts before file2. Natural sort is the standard choice for filenames and version strings.
Yes. It uses String.prototype.localeCompare, which honors the runtime locale's collation rules. For English that means dictionary order; for languages with special letters (German ß, Swedish å ä ö, Czech č š) the ordering matches local convention. The browser determines the locale from your OS settings unless overridden.
Yes. Modern JavaScript engines implement Array.prototype.sort as a stable Timsort variant, which has been the spec requirement since ES2019. Equal keys preserve their original relative order, which matters when you sort by one column and want the secondary order to remain the input order.
Each line is split on the delimiter you choose (tab, comma, pipe, or a custom string). The nth field becomes the sort key. The full line, including all other fields, is preserved in the output. If a line has fewer fields than the chosen index, it is treated as having an empty key and clumps at the start of the sorted list.
Yes. Enable the deduplicate option and any adjacent equal lines after sorting are collapsed to one. This is exactly the behavior of Unix `sort -u`. For order-preserving dedupe (without sorting), use the Remove Duplicates tool instead.
No. It uses a Fisher-Yates shuffle backed by Math.random(), which is fine for casual ordering but not for raffles, trial assignment, or anywhere bias matters. For cryptographic-grade randomness, generate the order with crypto.getRandomValues in a small script.
Configurable. By default, blank lines are kept in their original positions. You can also remove them entirely, push them all to the top, or push them all to the bottom of the result. This matters for poetry-shaped input where stanzas are separated by empty lines you may want to preserve.
Only if you enable trimming, which strips leading and trailing whitespace from each line before comparison. Otherwise the lines are written to the output exactly as they appeared in the input — the comparison key may be normalized (case-folded, trimmed) but the displayed text is verbatim.
Memory-bound only. Sorting one hundred thousand lines completes in well under a second; one million lines is interactive but takes a few seconds with locale-aware comparison. For multi-million-line inputs, prefer command-line `sort` since the browser will need a few hundred MB of memory.
Because alphabetical mode compares character-by-character. The string '100' starts with '1' (U+0031) which is less than '2' (U+0032), so '100' sorts before '2' alphabetically. Use numeric mode to extract the leading number, or natural mode if the lines mix letters and numbers.

Advertisement