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
- Finding lines that break a limit. Sort longest first and the offenders are at the top: subtitles over 42 characters, CSV fields over a column width, meta titles over 60. If you need the exact numbers rather than the order, the per-line character counter flags them directly.
- Writing and design. Sorting a list of headlines by length shows you which ones will wrap on a narrow card before you ever open the design file.
- Password and code audits. Sorting shortest first surfaces the entries that are suspiciously short.
- Data cleaning. Truncated rows and empty-ish junk collect at the short end where you can delete them in one selection.
- Word games and puzzles. Sorting a word list by length is the first step in most anagram and crossword work.
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.
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
- Paste your list into the box, one item per line.
- Press "Shortest first" or "Longest first".
- To make equal-length lines alphabetical too, press "Sort A→Z" first and then sort by length.
- Copy the result or download it as a .txt file.
Related tools
Frequently asked questions
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.
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.
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.
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.
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.