Skip to main content

Text Sorter

Sort lines alphabetically, numerically, by length or randomly.

Reviewed by · Last reviewed

How to Use the Text Sorter

  1. Paste your list into the input area, one item per line. Every line is treated as an atomic element regardless of whether it is a name, number, URL, file path, or free-form string.
  2. Pick a sort mode: A to Z for standard alphabetical, Z to A for reversed alphabetical, Short to Long or Long to Short for length-based sorting, Numeric for numeric prefix sorting, or Random Shuffle for Fisher-Yates randomization.
  3. Toggle options as needed: Case-sensitive keeps uppercase and lowercase apart, Remove duplicates runs a deduplication pass before sorting.
  4. Read the output panel below, which updates live. The item count is displayed so you can confirm deduplication removed the expected number of lines.
  5. Click Copy to grab the sorted result, or keep iterating by switching modes. The original input in the top box is never mutated.

How the Sorting Works

Alphabetical modes use Array.prototype.sort with a comparator built on Intl.Collator, which applies locale-aware collation: German ß sorts near ss, Czech ch sorts as a single letter in traditional ordering, Swedish ä sorts after z. When case-sensitive is off, the collator runs with sensitivity: "base" so accent and case differences collapse. Length sorts use a plain numeric comparison on a.length - b.length with a secondary alphabetical tiebreak so equal-length lines land in stable order. Numeric mode parses each line with parseFloat and falls back to 0 for non-numeric prefixes. Random shuffle uses the Fisher-Yates algorithm driven by crypto.getRandomValues for cryptographic-quality randomness, which prevents the bias that Math.random()-based shuffles can introduce.

When You Would Use It

  • Alphabetizing a contact list, guest list, or author list before exporting to a CSV.
  • Sorting a list of country codes or ISO currency codes into the order your API or UI expects.
  • Ordering Git branch names so the prefixes group together and are easier to scan.
  • Shuffling a raffle or test-group assignment list where fairness requires unbiased randomness.
  • Sorting log timestamps or Unix epochs numerically to build a rough timeline from a scrambled set.
  • Arranging imports in a source file by length so the shortest appear first - an aesthetic sometimes adopted by hand-tuned codebases.

Common Pitfalls and Edge Cases

  • Numeric strings without Numeric mode. 10 sorts before 2 in alphabetical mode because '1' precedes '2' as a character. Switch to Numeric or use a natural-sort-aware locale.
  • Mixed number and text lines in Numeric mode. Lines that do not begin with a number parse as NaN and are sorted to the top (treated as 0 for comparison). Strip or filter them if that is not what you want.
  • Locale assumptions. The default Intl.Collator uses the browser's locale, which may place accented characters differently than a server-side implementation. For cross-system consistency, set a specific locale in the URL or query string.
  • Line endings inside items. Sorting splits on \\n, so multi-line items (like a CSV row with an embedded line break inside a quoted field) are torn apart. Escape the newlines first or use a CSV-aware tool.
  • Invisible characters. Two lines that look identical but differ by a zero-width space sort as if different. The invisible-character detector on this site will help.
  • Stability. Array.prototype.sort is stable in modern V8, SpiderMonkey, and JavaScriptCore (required by ECMAScript since 2019), so equal keys preserve input order - useful when sorting by length and wanting original order within each length bucket.

Collation Background

Sorting human-readable text correctly is surprisingly subtle. A naive byte comparison sorts uppercase letters before lowercase, accented characters after z, and digits before letters - fine for a machine, bad for humans. Unicode Collation Algorithm (UCA), defined in Unicode Technical Standard #10, is the modern solution: it assigns multi-level weights to each character so primary-level comparison ignores case and accents, secondary level brings in accents, tertiary brings in case, and quaternary brings in punctuation. The JavaScript Intl.Collator API exposes UCA with locale overrides baked in, which is why a.localeCompare(b, "de-DE") gives linguistically correct German sorting. ICU (the International Components for Unicode library) provides the underlying data; Python's pyuca, Java's Collator, and C's strcoll all implement the same algorithm.

Comparison to Alternatives

On the command line, sort is the workhorse - lightning fast, streaming, and with flags for numeric (-n), reverse (-r), unique (-u), random (-R), and field-based (-k) sorting. Setting LC_ALL=C makes it use byte order, while setting a locale enables UCA-style collation. Excel and Google Sheets offer multi-column sorting with custom keys, which is what you want for structured data. SQL ORDER BY col COLLATE "en-US" moves the sort to the database and uses ICU. Python's sorted() with the key= argument gives full flexibility. Use this web sorter when your data is a flat line-per-item list, you want to toggle modes visually without writing a script, or you need unbiased randomness backed by the Web Crypto API rather than Math.random.

Frequently Asked Questions

Why does "10" sort before "2" in alphabetical mode?

Alphabetical sort compares character by character. The first character of "10" is "1", the first character of "2" is "2", and "1" has a smaller Unicode code point than "2", so "10" wins. To fix it, switch to Numeric mode, which parses the leading number and compares numerically. Some implementations offer "natural sort" that handles mixed alphanumerics like file1, file2, file10, file20 correctly; that is what macOS Finder uses, and it is available via Intl.Collator with numeric: true in modern browsers.

How secure is the random shuffle?

The shuffle uses the Fisher-Yates algorithm seeded by crypto.getRandomValues, which pulls bytes from the OS-level CSPRNG (cryptographically secure pseudo-random number generator) via the Web Crypto API. That is the same entropy source used for generating UUIDs, encryption keys, and session tokens in the browser. It is strictly better than Math.random, which is deterministic PRNG not suitable for fairness-critical tasks like random draws or test-group assignment.

Does my list get uploaded?

No. The sort runs as a synchronous JavaScript function inside your browser tab. There is no fetch, no service worker intercept, and no telemetry capturing the list. The Intl.Collator comparator and Array.prototype.sort are both native browser APIs that never touch the network. If your list contains confidential names or emails, sorting happens entirely on your device.

How do I sort by the second column of a CSV?

This tool treats each line as an atomic key, so multi-column sorting is not directly supported. You have three options: (1) reorder columns in your CSV first so the key is in column 1, (2) use a spreadsheet tool like Excel or Google Sheets which has built-in multi-column sort, or (3) use a CSV-aware CLI like csvkit's csvsort -c column-name. For simple key extraction in bash, awk -F, '{print $2 "|" $0}' | sort | cut -d'|' -f2- sorts by column 2 while preserving the full row.

Why does case-insensitive sort put lowercase and uppercase of the same letter in unpredictable order?

Because the comparator returns 0 for equal keys (a and A are equal under sensitivity: "base"), the survivors are ordered by their original position - that is, sort stability kicks in. If you want uppercase to always come first, switch to case-sensitive mode and accept Unicode code-point order. If you want a specific tie-breaker behavior, chain a second sort on top, or feed the output back through the tool with a stricter option.

How fast is the sort for big lists?

Native Array.prototype.sort is O(n log n) with a small constant factor; on a modern laptop browser it handles a million short lines in about a second. Beyond that you hit memory rather than CPU limits because the engine loads every line into a JavaScript string object. For multi-gigabyte files, use GNU sort, which is specifically tuned for external-memory sorting and can handle terabyte inputs with enough disk space.

Does Remove duplicates happen before or after sorting?

Before. The tool first builds a Set keyed by the normalized line (according to your case-sensitive and whitespace settings), keeps the first occurrence of each, then runs the sort on the survivors. This order matters: if sorting happened first and deduplication second, the "first occurrence" guarantee would refer to sorted order rather than input order, which is usually not what you want.

What happens with empty lines?

Empty lines are treated as valid items with an empty string value. In alphabetical mode they sort to the top because the empty string compares less than any non-empty string. In length mode they end up at the start of Short-to-Long or end of Long-to-Short. In Numeric mode they parse as NaN and fall back to the 0 bucket. If you do not want them in the output at all, use the Remove Blank Lines option on the Whitespace Remover first.

Is the sort deterministic?

Yes, except for Random Shuffle. Alphabetical and numeric modes produce the same output for the same input every time you run them, in any browser, with the current locale. Random Shuffle is explicitly non-deterministic; each click produces a new order seeded by fresh crypto bytes. If you need a deterministic shuffle for a reproducible demo, run it once and save the output.

Can I sort dates?

Only if their format makes alphabetical order equal chronological order. ISO 8601 (2026-04-22T10:30:00Z) is designed for this and sorts correctly in alphabetical mode. Formats like 4/22/2026 do not; convert them or sort the Unix epoch numerically.

More Text Tools