Sort lines by length

Orders lines by how many characters they contain, shortest or longest first.

Paste a list and choose a direction. Lines are measured in characters and reordered, and lines of equal length keep the order you gave them.

Why the sort is stable, and why that matters

Most short implementations sort with a plain comparison and let equal values fall wherever the browser puts them. That means two lines of identical length can swap places every time you press the button, which looks like a bug and makes the output impossible to diff against a previous run.

This one is stable: if apple and grape are both five characters and apple came first in your input, it comes first in the output. Every run produces the same result. Combined with an alphabetical pass first, you get a predictable secondary ordering — sort A→Z, then sort by length, and equal-length lines end up alphabetical inside each length group.

How length is measured

Length counts characters, including spaces and punctuation. Leading and trailing whitespace counts too, which occasionally surprises people whose lines were copied out of a spreadsheet with trailing tabs — run trim spaces first if that is your case.

Characters are counted the way a person counts them, not the way naive code does. An emoji or an accented character built from combining marks counts as one, not two or three. This matters if you are checking lines against a platform limit, because a list sorted by a broken counter puts the wrong lines at the top.

What this is actually for

Doing it without this tool

Notepad++: the plugin TextFX had a length sort, but TextFX does not work in 64-bit Notepad++, which is what everyone runs now. The modern answer is the Python Script plugin or a column of lengths — neither is quick.

Excel: add a helper column with =LEN(A1), fill down, then sort the sheet by that column and delete it afterwards. It works, but it turns a five-second job into a small chore, and pasting multi-line text into Excel splits it in ways you then have to undo.

Command line: awk '{print length, $0}' file.txt | sort -n | cut -d' ' -f2- sorts shortest first; add -r to sort for longest first. Elegant if you already live in a terminal.

VS Code: no built-in length sort. Extensions add one, but installing an extension to reorder twenty lines is not a trade most people want to make.

Try:
0Words
0Characters
0No spaces
0Sentences
0Paragraphs
0Lines
0Pages
0Reading
0Speaking

Runs entirely in your browser. Your text is never uploaded — open DevTools, Network tab, and you will see zero requests while you use this tool.

How to use it

  1. Paste your list into the box, one item per line.
  2. Press "Shortest first" or "Longest first".
  3. To make equal-length lines alphabetical too, press "Sort A→Z" first and then sort by length.
  4. Copy the result or download it as a .txt file.

Related tools

Sort linesAlphabetical or reverse sorting for any list, with natural number order.Remove empty linesDeletes blank lines — or collapses runs of them down to one.Remove duplicatesKeeps the first occurrence of every line and preserves the original order.Remove stop wordsStrips common English filler words and leaves the words that carry meaning.Fancy textOne box in, twelve Unicode styles out. Copy the one you want.Instagram caption lengthThe hard limit is 2,200 characters. The one that matters is the 125 you see…Group generatorPaste names, choose how many groups, get evenly sized random teams.

See all text tools →

Frequently asked questions

Are spaces included in the length?

Yes. Every character counts, including spaces, punctuation and any leading or trailing whitespace. If trailing spaces are skewing your result, remove them first with the trim spaces tool.

What happens to lines of the same length?

They keep the order they had in your input. The sort is stable, so repeated runs always produce the same output and you can chain an alphabetical sort before a length sort to control the secondary order.

Do emoji count as one character?

Yes. Length is measured in visible characters, so an emoji, a flag or an accented letter counts as one even though it may be several bytes internally. Many other tools count these as two or more.

Can I sort by number of words instead of characters?

Not on this page. The per-line counter tool shows both character and word counts for every line, which covers most cases where word length matters.

Are empty lines removed?

No. Empty lines have length zero and collect at the top when sorting shortest first. Use the remove empty lines tool if you want them gone.