Skip to content

Tool 35 / Format lines

Put the lines in order.

Sort a list, a table, or a pile of filenames by the value that matters. Choose the mode, direction, and tie behavior, and keep the header where it belongs.

  • Free to use
  • Three sort modes
  • Runs locally
Live workbenchAscending · Whole line

Compare each complete line, letter by letter, with optional natural number ordering.

Looking for shortest or longest first? Use the dedicated length sorter.

Sorted lines
4 lines in4 lines out0 blank removed0 duplicates removedLocal sort · never uploaded

What it does

This is a general line sorter. It takes one item per line and returns the same items in a new order. Unlike a spreadsheet sort, it works on plain text that you can paste from anywhere, and unlike a one-line command, it lets you choose the comparison without remembering a flag.

There are three keys: the whole line, the last word, and one field from a delimited row. Each can run ascending or descending. Blank lines, duplicates, surrounding spaces, and a header row are handled by explicit options rather than silent guesses. The output is text, so it can go back into a document, a code block, or another tool.

Sort modes

Whole line is the ordinary alphabetical sort. It compares the complete line with the browser’s collation rules, which means letters with accents and letters in other scripts are ordered sensibly rather than by raw code point. Punctuation is not ignored, so a line beginning with a quote will sort near other punctuation.

Length is handled separately. If shortest-first or longest-first is the task, use Sort Lines by Length. That page measures visible characters with grapheme clusters, keeps equal lengths in a stable alphabetical order, and has its own controls for headers and blank lines.

Last word splits on whitespace and compares the final token. It is a quick way to sort names written as “First Last” without rearranging them, or to group filenames by extension when the extension is the last token. It is whitespace-based, not a name parser.

Delimited column splits each line by the separator you provide and compares one field. Column numbers start at 1. A missing field sorts as an empty value. This is a line-level sort, not a full CSV parser: a quoted delimiter inside a field is still treated as a separator here.

Ties, case, and numbers

Natural number ordering compares runs of digits as numbers, so item 2 comes before item 10. Turn it off for a strict character comparison, which is sometimes what a machine-readable identifier needs. The case-sensitive option changes the collator’s sensitivity; the output always keeps the original capitalization.

Trimming removes spaces and tabs from both ends of each line before comparison and writes the trimmed line to the output. That makes “ Apple” and “Apple” compare equally, but it also removes intentional indentation. Remove-empty and dedupe are separate switches. Dedupe keeps the first line in the sorted result, not necessarily the first line in the input.

Examples

Sort names by the last word
Mode: last word · ascending

Before

Ada Lovelace,Research,London
Grace Hopper,Engineering,New York
Alan Turing,Research,Manchester

After

Ada Lovelace,Research,London
Alan Turing,Research,Manchester
Grace Hopper,Engineering,New York

The last word is the city in this data, so the rows end up in London, Manchester, New York order. The names themselves are not rearranged. If a row has no spaces, its only word is also its last word, which makes the mode safe for single-token lists.

Sort a CSV by its second column
Mode: delimited column · column 2 · no header

Before

Ada Lovelace,Research,London
Grace Hopper,Engineering,New York
Alan Turing,Research,Manchester

After

Grace Hopper,Engineering,New York
Ada Lovelace,Research,London
Alan Turing,Research,Manchester

Engineering sorts before Research, and the two Research rows keep their relative input order. If the first row were a header, “Keep the first line as a header” would hold it in place and sort only the data below it. The output is still a line-oriented text block, not a rewritten CSV file.

Natural number order
Natural ordering on · ascending

Before

item 10
item 2
item 1

After

item 1
item 2
item 10

With natural ordering off, the same input becomes item 1, item 10, item 2, because the character “1” in 10 sorts before “2”. Neither order is wrong; they answer different questions.

Limits worth knowing

  • It sorts lines, not records. A newline inside a quoted CSV field becomes a separate line. Use the delimited column extractor for quoted rows before sorting if the file contains multiline fields.
  • Column mode is simple splitting. It does not unquote fields, respect escaped delimiters, or understand a header schema. A comma inside a quoted name still splits the row. That trade-off keeps the tool predictable for logs and simple exports.
  • Collation depends on the browser. The ordering uses the runtime locale, so two machines can differ on edge cases involving accents, punctuation, or language-specific alphabets. For a byte-stable order, use a dedicated program with an explicit locale.
  • Dedupe is not a merge. It drops later copies and keeps the first one in the sorted result. It does not combine fields or choose the “best” duplicate.
  • Trimming changes the output. The option is convenient for pasted lists and destructive for indented code. Leave it off when leading whitespace is content.

Page updated

Questions, answered

How is this different from Sort Lines by Length?

This page sorts whole lines alphabetically, by the last word, or by a delimited column. Length is a different key, so it has a dedicated page: Sort Lines by Length orders lines by grapheme count and is the better choice for overflow checks, poetry, or subtitle review.

Does “natural number order” mean 2 comes before 10?

Yes. With natural ordering on, runs of digits are compared as numbers, so item 2 comes before item 10. With it off, the comparison is character by character, where the “1” in 10 comes before the “2” in 2. Natural ordering is usually what a human expects for filenames and version-like labels.

Is the sort case-sensitive?

No, by default. The browser’s collator uses base-letter comparison, so Apple and apple compare as the same word, and accent differences are also ignored at that setting. Turn on “case sensitive” when the capitalization is part of the key. The output preserves the original spelling either way.

What happens when two lines have the same key?

The order is deterministic rather than random. Whole-line, last-word, and column ties keep the input order after the primary comparison. That means running the same sort twice on the same input gives the same output, which matters when you are reviewing a diff.

Can I sort a CSV without breaking its header?

Yes, if the header is the first line. Turn on “Keep the first line as a header” and choose the delimited-column mode with the matching separator and column number. The header is held in place and the data rows are sorted. The tool does not parse quoted newlines inside a field; use the column extractor first if the CSV has multiline fields.

Does it remove duplicates automatically?

No. Duplicate removal is a separate checkbox and is off by default. When it is on, the comparison is case-insensitive unless you enabled case sensitivity, and the first occurrence in the sorted result is kept. The statistics bar reports how many lines were removed.

All tools →