How to Sort a List by Length (Shortest or Longest Line First)
Sort lines by character length, shortest or longest first, online or with a SORTBY and LEN formula in Excel or Google Sheets. Includes how spaces and ties work.
The short answer
To sort a list by length, paste it into Sort Text with one item per line, choose Shortest first or Longest first, and click Sort Lines. Length means the number of characters on the line, including spaces. Lines of equal length keep their original order. In a spreadsheet, use =SORT(A2:A100, LEN(A2:A100), TRUE) in Google Sheets or =SORTBY(A2:A100, LEN(A2:A100), 1) in Excel 365.
When sorting by length is useful
Alphabetical order is the usual choice, but length order solves a different set of problems:
- Finding lines that break a limit. Sort page titles, ad headlines or product names longest first, and everything over the limit is at the top.
- Keyword research. Sorting a keyword list longest first brings long-tail phrases ("waterproof hiking shoes for women wide fit") to the top and short head terms to the bottom.
- Spotting junk. Very short lines (single letters, stray numbers) and very long ones (two items pasted together on one line) stand out at each end of the list.
- Layout and design. Labels, menu items or table headings sorted longest first show you the widest text you need to fit.
- Word games and puzzles. Group words by length for crosswords or spelling practice.
How to do it with Sort Text
Worked example
Input, one item per line: banana, fig, apple pie, kiwi, an empty line, cherry.
Shortest first gives:
Longest first (without the empty line) gives apple pie, banana, cherry, kiwi, fig.
Banana and cherry are both six characters. In both directions they stay in the order they were typed (banana before cherry), because the sort is stable: ties are never shuffled.
What counts towards length
The tool measures the full line exactly as pasted:
| Line | Length | Note |
|---|---|---|
| fig | 3 | Letters only |
| fig followed by a space | 4 | Trailing spaces count |
| apple pie | 9 | The space in the middle counts |
| (empty line) | 0 | Sorted first in Shortest first |
| 👍 | 2 | Most emoji count as two characters |
Three things often surprise people:
Trailing spaces change the order
A line copied from a spreadsheet or PDF may end in a space you cannot see. "fig " (with a space) is 4 characters, so it sorts after "fig" and level with "kiwi". If the order looks slightly wrong, remove trailing spaces first: in Find and Replace, tick Use regex, find [ \t]+(?=\n|$) and replace with nothing.
Duplicates must match exactly
The Remove duplicates option removes lines that are character-for-character identical. "fig" and "fig " are different, and so are "Fig" and "fig". For case-insensitive or space-insensitive de-duplication, run the list through Remove Duplicate Lines first, which can trim spaces and ignore case.
Empty lines go to the top
Blank lines have a length of zero, so in Shortest first they all collect at the start, and in Longest first at the end. Delete them before sorting, or remove them from the result.
Sorting by length in a spreadsheet
If your list is already in a spreadsheet, a formula keeps the sorted copy up to date when the list changes.
Google Sheets
=SORT(A2:A100, LEN(A2:A100), TRUE) sorts shortest first. Change TRUE to FALSE for longest first.
Excel (Microsoft 365 and Excel 2021)
=SORTBY(A2:A100, LEN(A2:A100), 1) sorts shortest first. Use -1 for longest first.
Older Excel versions
Add a helper column with =LEN(A2), fill it down, then select both columns and sort by the helper column using Data, Sort.
Spreadsheets count characters the same way for normal text, but blank cells inside the range are also sorted, so trim the range to your data.
Sorting by length in code
For developers, the same result takes one line:
- Python:
sorted(lines, key=len)for shortest first, or addreverse=Truefor longest first. Python's sort is stable, so ties keep their order. - JavaScript:
lines.sort((a, b) => a.length - b.length). Swap a and b for longest first. Modern JavaScript sorts are stable too.
Note that both count characters the way the language stores them. In JavaScript, as in the tool, most emoji have a length of 2.
Sort by length, then alphabetically
Sometimes you want groups of equal length to be in A to Z order. Sort Text applies one sort at a time and keeps the existing order for ties, so you can combine two passes:
Because ties keep their previous order, each length group is now alphabetical. The same trick works in reverse to sort alphabetically within each length, longest first.
Other sort orders
Sort Text also sorts A to Z, Z to A, and numerically. If a list of numbers sorts as 1, 10, 2, read why 10 comes before 2 when sorting and use the Numeric option instead. For alphabetical sorting inside Word or Google Sheets, see how to sort a list alphabetically.
The list is sorted in your browser; nothing you paste is uploaded.
Frequently asked questions
How do I sort a list by length online?
Paste the list into Sort Text with one item per line, choose Shortest first or Longest first, and click Sort Lines. Length is the number of characters, including spaces.
How do I sort by length in Excel?
In Microsoft 365 or Excel 2021, use =SORTBY(A2:A100, LEN(A2:A100), 1) for shortest first or -1 for longest first. In older versions, add a =LEN(A2) helper column and sort by it.
How do I sort by length in Google Sheets?
Use =SORT(A2:A100, LEN(A2:A100), TRUE) for shortest first, or FALSE for longest first.
What happens to lines with the same length?
They keep their original order. You can use this to sort alphabetically within each length: sort A to Z first, then sort by length.
Do spaces count when sorting by length?
Yes. Spaces inside the line and at the end both count, so a line with a trailing space sorts as one character longer.
Put this guide into practice with our free online tool — no signup required.
Open tool