Skip to content

Tool 21 / Sort

Put the long lines where you can see them.

Sort a list by how much space each item takes. Longest-first finds the line that will break the layout; shortest-first finds the tags that get lost in the crowd.

  • Free to use
  • Stable ties
  • Runs locally
Live workbenchShortest first

Equal-length lines fall back to alphabetical order, so the result is stable and easy to scan.

Sorted by length
6 lines in6 lines out1 chars shortest25 chars longestNever uploaded

What it does

Sort Lines by Length measures each line and reorders the list by that measurement. It does not change the text of any line. A line with trailing spaces is longer than the same line without them, which is why the trim option is on by default: it measures the content, not the accidental padding.

Length is counted in visible characters, using grapheme clusters. An emoji is one character, not a surrogate pair. A letter with a combining accent is one character. That makes the ordering match what a reader would call “long”, rather than what a byte-oriented tool would report.

This page is deliberately only about length. To sort whole lines alphabetically, by the last word, or by a CSV column, use the general line sorter instead.

Ties and stable order

Equal-length lines are sorted alphabetically as a tie-breaker. This is a small detail with a large effect: without it, the order of same-length lines would be arbitrary and could change from one run to the next. With it, the output is deterministic and easy to scan.

The alphabetical tie-breaker is locale-aware. In English, “apple” comes before “Banana” under a case-insensitive comparison; the exact ordering of accented letters depends on the browser’s collation. If you need a strict code-point order, sort the output again in a tool that exposes that choice.

Headers and duplicates

A header row is not part of the data. “Keep the first line as a header” takes the first line out of the sort and restores it at the top. It does not inspect the line to see whether it looks like a header; if you turn it on by accident, the first data row is treated as a title.

“Remove duplicate lines after sorting” is a convenience, not a replacement for a real deduplication pass. It removes exact duplicates after the edge spaces have been trimmed, and it runs after the sort so the shortest copy is the one that survives — which is usually the same as any other copy, but not always. For a careful dedupe with case and order controls, useRemove Duplicate Lines.

Examples

Shortest first
Trim on · blanks removed

Before

The quick brown fox
A
jumped over
Two words
extraordinarily long line

After

A
Two words
jumped over
The quick brown fox
extraordinarily long line

The one-character line comes first, then the two-word line, then the longer phrases. The ordering is by character count, not by word count, so “Two words” (nine characters) comes before “jumped over” (eleven characters).

Longest first for a layout check
Longest first · header kept

Before

Product names
Mug
Notebook
Limited-edition enamel camping mug

After

Product names
Limited-edition enamel camping mug
Notebook
Mug

The header stays on top, and the longest product name rises to the top of the data. That is the line most likely to overflow a card, a table cell, or a fixed-width export.

Limits worth knowing

  • Character length is not visual width. A wide letter such as W takes more horizontal space than an i, even though both count as one character. For a true visual fit, measure the rendered text in the target font.
  • Trailing spaces count unless trimmed. The default trim keeps the measurement about the content. If the spaces are meaningful, turn it off and expect them to affect the order.
  • Blank lines have length zero. In shortest-first mode they jump to the top unless removed. That is why “remove blank lines” exists as a separate choice.
  • Sorting is not grouping. Lines of similar length end up near each other, but the tool does not create buckets or labels. It does not know that two lines are related just because they are the same length.
  • Nothing is stored. A refresh clears the input and settings. Copy the result before leaving.

Page updated

Questions, answered

Is length counted in characters or bytes?

Characters, counted as grapheme clusters so an emoji or an accented letter counts as one visible character. It is not a byte count and it is not a word count. A line with ten letters and three spaces is thirteen characters.

What happens when two lines have the same length?

They fall back to alphabetical order, so the result is stable and readable. Without a tie-breaker, the order of equal-length lines would depend on the sort implementation and could change between runs.

Can I keep the header row at the top?

Yes. “Keep the first line as a header” removes the first line from the sort and puts it back at the top. Use it for a list with a title or a column heading. It does not detect whether the first line is actually a header; it trusts you.

Why would I sort lines by length?

Longest-first is a quick way to find the line that will overflow a layout, a tweet, or a database column. Shortest-first is useful for scanning tags, codes, or glossary entries. It is also a rough way to group list items by how much detail they contain.

Does it remove blank lines automatically?

No. “Remove blank lines” is an option and is off by default. A blank line has length zero, so it would otherwise sort to the top in shortest-first mode. Turn the option on when the blanks are noise, or leave it off when the blank line is a deliberate separator.

All tools →