The short answer. Paste your lines into the list alphabetizer and pick Length mode — shortest first, one click. In VS Code with the Sort Lines extension installed, the command Sort Lines (Line Length) does the same. On the command line, awk '{ print length, $0 }' file.txt | sort -n | cut -d' ' -f2- — or sort -rn in the middle to get longest first. Excel and Word need a helper column with =LEN(A1); then sort by that column. The table at the bottom maps your situation to the right method.
When sorting by length is the right answer
Length sort is a niche tool, but the cases where it's exactly the right operation are real:
- Finding the longest URL or path in an export — longest line goes to the bottom (or top, with descending).
- Spotting log lines with outsized payloads — in a log, an exceptionally long line is usually an error trace or a binary dump.
- Cleaning a list of titles for a column-width-constrained UI — sort by length to see what fits.
- Finding the shortest filename in a set — useful when designing slug schemes or testing column widths.
- Identifying outliers in a CSV column after extracting it as plain lines — outliers tend to be the longest or shortest entries.
"Length" here means character count by default in every tool below — not display width, not byte count. For East Asian / RTL / emoji-heavy text those can disagree noticeably; the FAQ at the bottom covers how to force a byte-only count for size-limit checks.
How do you sort by length online, instantly?
Open the list alphabetizer. Paste your list. Pick Length from the mode buttons. Copy the result.
- Shortest first. The default Length mode sorts ascending by character count.
- Honest limitation: no built-in descending mode. If you want longest-first, sort ascending then reverse the result manually, or use the alphabetizer twice in sequence (Length, then Z→A to reverse). We may add a "Length descending" toggle later; for now, the workaround works.
- Dedupe + Trim toggles still apply. Both still affect the comparison, so dedupe-while-sorting-by-length is one click away.
- Nothing uploads. Open DevTools → Network to confirm: zero outbound requests carrying your list.
How do you sort by length in VS Code?
VS Code doesn't ship with a sort-by-length command, but the popular Sort Lines extension by Tyriar does.
- Install Sort lines by Tyriar from the Extensions marketplace (already installed for most VS Code users who write code).
- Select the lines to sort.
Ctrl+Afor the whole file. - Open the Command Palette (
Ctrl+Shift+P) and run Sort Lines (Line Length).
The extension command sorts ascending (shortest first). For longest-first there's no built-in descending command at the time of writing — sort ascending, then use the extension's Reverse Lines command, or roll your own with the command-line recipe below.
Without the extension, VS Code has no native sort-by-length command. Copy the lines into the browser tool or use the command-line recipe below.
How do you sort by length in Notepad++?
Notepad++ has built-in sort by alphabet and by integer (for lines that start with numbers), but not by line length. Two practical paths:
- Copy the lines into a tool that does support length sort (the browser tool or VS Code with the Sort Lines extension), then paste the result back into Notepad++.
- Use Notepad++'s Python Script plugin (or any plugin with eval support) to run a one-liner:
lines.sort(key=len).
For one-off sorts, the copy-to-another-tool route is faster than configuring a plugin. For repeated work, learn the command-line recipe below.
How do you sort by length on the command line?
The Unix idiom uses awk to prefix each line with its length, then a numeric sort, then cut to drop the prefix.
Shortest first (ascending):
awk '{ print length, $0 }' file.txt | sort -n | cut -d' ' -f2-
Longest first (descending):
awk '{ print length, $0 }' file.txt | sort -rn | cut -d' ' -f2-
Breakdown:
awk '{ print length, $0 }'prefixes each line with its length followed by a space, then the line itself.sort -nsorts numerically (so 10 follows 9, not 1).-rnreverses to longest first.cut -d' ' -f2-takes the second field onwards (the original line, without the length prefix).
For the longest line only (no full sort needed):
awk 'length > max { max = length; line = $0 } END { print max, line }' file.txt
Prints just the maximum length and the line that achieved it — faster than a full sort on big files.
On Windows in PowerShell:
Get-Content file.txt | Sort-Object Length
Add -Descending for longest first. PowerShell sorts by the natural property of the line object, which for strings is character length.
How do you sort by length in Excel or Google Sheets?
Both spreadsheets need a helper column with the line lengths, then sort by that column.
- In B1, type
=LEN(A1)and fill down. - Select both columns A and B.
- Data → Sort → Sort by Column B → Smallest to Largest (or Largest for descending).
- Delete the helper column when you're done.
For Excel 365 / 2021 with dynamic arrays, the cleaner one-liner:
=SORTBY(A2:A100, LEN(A2:A100))
Sorts the A range by the length of each cell, ascending. Pass -1 for descending: =SORTBY(A2:A100, LEN(A2:A100), -1). No helper column, no manual cleanup.
Which method should you use?
| Your situation | Best method |
|---|---|
| One-off list, want shortest first | Browser tool in Length mode |
| One-off list, want longest first | Browser tool Length mode → copy → paste back → Z→A; or awk ... | sort -rn |
| Open in VS Code | Sort Lines extension → Sort Lines (Line Length) |
| Excel column with related data | =SORTBY(A2:A100, LEN(A2:A100)) (modern Excel) |
| Excel without dynamic arrays | Helper column =LEN(A1), sort by it |
| File on disk, scripted | awk '{ print length, $0 }' file.txt | sort -n | cut -d' ' -f2- |
| Just want the longest line (no full sort) | awk 'length > max { max = length; line = $0 } END { print max, line }' |
| 1 GB log file | The awk + sort pipeline — streams, no memory ceiling |
Frequently asked questions
Why are tab characters sometimes counted differently?
Length usually counts characters, where a tab is one character. Display width counts visual columns, where a tab might be 4 or 8 columns. Most tools (the browser tool, awk length, Excel's LEN) use character count; if you're sizing for a fixed-width display, you may want to expand tabs first — expand file.txt on Linux / macOS converts tabs to spaces before piping into the awk recipe. There's no equivalent built into Windows; PowerShell's -replace "`t", " " does the same job.
Why does sort-by-length put line 9 above line 10 when they're labels?
It doesn't — you're seeing a length sort, not a numeric sort. "Item 9" is 6 characters; "Item 10" is 7. They sort in the right order. But "Banana" (6 characters) would also be shorter than "Item 10" (7), regardless of the labels. If you want numeric sort, use Natural mode in the alphabetizer, or sort -V on the command line — not Length mode.
How do I sort by word count (not character count)?
On the command line:
awk '{ print NF, $0 }' file.txt | sort -n | cut -d' ' -f2-
NF is the number of fields (words, by default whitespace-separated) on each line. Same recipe as length sort, swapping length for NF. In Excel, use a helper column with the word-count formula from the count words guide, then sort by it.
Can I sort by the length of a specific field, not the whole line?
Yes, on the command line. For a CSV where you want to sort by the length of the third field:
awk -F, '{ print length($3), $0 }' file.csv | sort -n | cut -d' ' -f2-
-F, tells awk to split on commas; length($3) takes the length of the third comma-separated field. In Excel, a helper column with =LEN(C2) does the same.
What's "length" for emoji and East Asian characters?
Inconsistent across tools. JavaScript's String.length counts UTF-16 code units — most emoji are 2 units (a surrogate pair), so "🎉".length === 2. Excel's LEN counts the same way. awk length in gawk and mawk returns characters under a UTF-8 locale (the common Linux / macOS default), not bytes. If you need exact byte count, force a byte-only locale on the shell side: LC_ALL=C awk '{ print length, $0 }' file.txt — the prefix is a shell environment variable, not awk syntax, and only works when set before awk starts.
Is it safe to paste a sensitive list into an online length sorter?
Only if the tool runs entirely in the browser. Open DevTools, switch to the Network tab, paste, sort, watch for outbound requests. If any carry your list, the page sent it to a server. The joinlines alphabetizer makes zero requests after page load — verifiable in DevTools.
— the joinlines team, 2026-04-30