Sort lines
Sort lines of text A to Z or Z to A, in natural order (file2 before file10), by code point, by number or by length.
Code examples
Copy and paste these into your own project. Each one uses only the language's standard library, so there is nothing to install.
const lines = ['file10', 'file2', 'file1']
const sorted = lines.toSorted(new Intl.Collator('en', { numeric: true }).compare)
console.log(sorted.join('\n'))What it does
Sorts lines of text. Natural order puts file2 before file10; the other orders compare text the way your language does, compare raw code points, use the number a line starts with, or use its length. You can also reverse the list without sorting it.
How to use it
- Type or paste one item per line, or drop a text file.
- Pick an order. Turn on Z to A to sort backwards, Case sensitive to treat
aandAas different, or Ignore surrounding whitespace to compare lines without their leading and trailing spaces. The lines themselves stay as you typed them. - Copy or download the sorted list. Remove blank lines drops empty lines first.
Examples
Natural order against plain text order:
file10 file1
file2 → file2
file1 file10
Numeric order uses the number a line starts with, so 10 apples follows 2, and a line with no number goes last.
Good to know
- Sorting is stable: lines that compare equal keep their order, in both directions.
- Natural and A–Z text order use English rules. Code point order is the same everywhere, which makes it the one to pick when another program has to agree with the result.
- In code point order every capital letter comes before every lowercase one, unless you turn case sensitivity off.
- A single line break at the end of the text ends the last line rather than adding an empty one.
What is natural sort order?
A plain alphabetical sort compares text one character at a time, so file10 lands before file2. Natural order reads the digits as numbers, so file2 comes before file10, the way a person would sort the list. It is what file managers use.
Frequently asked questions
What is the difference between A–Z text and code point order?
A–Z text follows the rules of a language: é sits next to e and case differences come last. Code point order compares the raw character codes, so every capital letter comes before every lowercase one and é lands after z. Pick it when another program expects that order.
How does numeric order treat lines that are not numbers?
It sorts by the number a line starts with, so 10 apples comes after 2. Lines that start with no number follow the ones that do, in code point order.
Does this tool send my lines anywhere?
No. Sorting happens in your browser. What you type is never sent to our servers.