Skip to content
Toolcroft

Sort Lines - Sort Text Lines Online

Text Tools

Sort lines of text alphabetically, numerically, by length, or randomly. Ascending or descending, case-insensitive or case-sensitive. Free and runs entirely in your browser.

How to use this tool

  1. Paste your text. Paste or type the lines you want to sort into the input box.
  2. Choose sort method. Select Alphabetical, Numeric, By Length, or Random from the method dropdown.
  3. Set order and options. Choose Ascending or Descending. Toggle case-sensitive or trim whitespace as needed.
  4. Copy the result. Click "Copy result" to copy the sorted text to your clipboard.

When to sort lines of text

Sorting lines is a surprisingly common task: alphabetising a list of names, putting import statements in order, ranking items by length, or simply randomising a list to remove bias. A dedicated tool saves the effort of copy-pasting into a spreadsheet or writing a one-off script.

Sort methods explained

  • Alphabetical: the default. Lines are sorted lexicographically using the browser's built-in locale-aware comparison. Works well for any language.
  • Numeric: lines are compared by their leading numeric value. "2" sorts before "10" (unlike alphabetical, where "10" would come first). Non-numeric lines are sorted after all numeric lines.
  • By length: shorter lines first (ascending) or longer lines first (descending). Lines of equal length are sub-sorted alphabetically.
  • Random (shuffle): randomises the order of lines using the browser's Math.random(). Useful for creating randomised quizzes, picking a random winner from a list, or removing presenter bias from a slide order.

Case sensitivity

In case-insensitive mode (the default), "Apple", "apple", and "APPLE" sort to the same position. In case-sensitive mode, uppercase ASCII letters (A–Z) sort before their lowercase equivalents (a–z), so "Apple" sorts before "banana".

Trim whitespace option

When enabled, leading and trailing spaces are stripped from each line before comparing, but the original line (including its spaces) is preserved in the output. This is useful when pasting from sources that add inconsistent indentation.

Natural sort (file ordering)

Standard alphabetical sorting treats strings character by character, which produces counter-intuitive results for filenames and numbered sequences: "file10" sorts before "file2" because "1" < "2" in ASCII. Natural sort fixes this by comparing embedded numeric substrings as integers, so "file2" sorts before "file10". This tool's Numeric sort mode applies this logic to leading numbers; many text editors and file explorers use natural sort for their listings.

Locale-aware sorting

Different languages have different collation rules. Swedish treats Å, Ä, Ö as distinct letters that sort after Z. German sorts ä as equivalent to ae in traditional collation. The browser's Intl.Collator API handles these rules when locale-aware sorting is enabled. Without locale awareness, characters are compared by Unicode code point - which often mispredicts the intended order for non-English text.

Deduplication

To remove duplicate lines after sorting, use the Remove Duplicate Lines tool. Sorting first (which places duplicates adjacent to each other) makes deduplication more predictable.

Frequently asked questions

Does this tool sort stably?

Yes. JavaScript's built-in Array.sort() is guaranteed to be stable in all modern browsers (V8, SpiderMonkey, JavaScriptCore). Lines that compare as equal will remain in their original relative order.

How does numeric sort work?

Lines that start with a valid number are sorted by that numeric value. Lines that cannot be parsed as a number are placed after all numeric lines and sorted alphabetically among themselves.

What is the difference between case-sensitive and case-insensitive sort?

In case-insensitive mode (the default), "Apple" and "apple" are treated as equal. In case-sensitive mode, uppercase letters sort before lowercase in ASCII order, so "Apple" comes before "banana".

Is there a line limit?

There is no enforced limit. Very large inputs (hundreds of thousands of lines) may be slow on low-powered devices since all processing runs in the browser tab.

Browse all text tools