Sortiert wird sprachbewusst statt über rohe Zeichenwerte, sodass Buchstaben mit Akzent und nichtlateinische Zeichen dort landen, wo eine Leserin dieser Sprache sie erwartet, statt hinter Z geschoben zu werden. Fünf Ordnungen stehen bereit: alphabetisch, natürlich, numerisch, nach Länge und als Mischung. Die ersten vier sind stabile Ordnungen, die Sie umkehren können; die Mischung ist ein Fisher-Yates-Durchlauf über die Liste, der jeder Anordnung dieselbe Chance gibt und die Richtungseinstellung vollständig ignoriert.
Die natürliche Ordnung behandelt Ziffernfolgen innerhalb einer Zeile als Zahlen, weshalb sie file2 vor file10 stellt, was die rein alphabetische Ordnung nicht tut. Die numerische Ordnung sucht die erste Zahl irgendwo in jeder Zeile — negative und Dezimalwerte eingeschlossen — und sortiert danach; Zeilen ohne Zahl wandern ans Ende. Die Sortierung nach Länge vergleicht Zeichenzahlen und entscheidet Gleichstände alphabetisch, damit gleich lange Zeilen dennoch vorhersehbar herauskommen.
Eine Ordnung wählen, die zu Ihren Daten passt
-
Alphabetisch ist Zeichenreihenfolge, nicht menschliche Reihenfolge:
Alles mit Zahlen darin — Versionen, Dateinamen, Rechnungsnummern — sortiert nach Ziffer statt nach Wert. Die natürliche Ordnung behebt das.
-
Numerisch liest die erste Zahl, die es findet:
Eine Zeile, die mit einem Datum oder einer Kennung beginnt, sortiert nach dieser Zahl statt nach der gemeinten. Für gemischte Zeilen ist die natürliche Ordnung oft die bessere Wahl.
-
Absteigend kehrt nach dem Sortieren um:
Das schiebt auch die Zeilen ohne Zahl vom Ende an den Anfang.
-
Die Mischung ist bei jedem Durchlauf wirklich zufällig:
Lassen Sie sie zweimal laufen, erhalten Sie zwei verschiedene Anordnungen. Richtung und Groß- und Kleinschreibung spielen dabei keine Rolle.
Alphabetical is wrong for numbered data
This is why a natural sort option exists, and it is the thing worth understanding.
A plain alphabetical sort compares character by character. That puts item10 before item2, because the character 1 comes before 2. Everything with a number in it comes out in an order nobody wants: version numbers, file names, chapter headings, invoice references.
A natural sort recognises runs of digits and compares them as numbers, so item2 precedes item10. The [msm_tool slug="sort-lines"] offers both because both are correct for different data.
Case sensitivity changes the order
A case-sensitive sort puts all capitals before all lowercase, because that is their order in the character set. So Zebra comes before apple, which looks like a bug and is not.
For a list of names, titles or anything a person will read, a case-insensitive sort is almost always what you want. For sorting identifiers or code, case sensitivity may matter.
Accented characters sort after Z
A simple sort orders by code point, and accented Latin characters sit above the plain A–Z range. So Ångström lands after Zulu rather than at the beginning where a Swedish reader would expect it — and a French reader would expect é to sort with e, which it does not.
Proper alphabetical ordering is language-specific and genuinely complicated: Swedish puts Å after Z deliberately, German does not, and Spanish used to treat ll as a single letter. A code-point sort is predictable rather than linguistically correct, and for a list anyone will read in a specific language, expect to adjust it by hand.
Removing duplicates while sorting
Sorting groups identical lines together, which makes deduplication trivial — and the comparison is exact. A trailing space makes two visually identical lines different, and they will both survive.
If a deduplication leaves entries you were sure were duplicates, clean the whitespace first and sort again.
Blank lines and the header row
Two practical traps:
- Blank lines sort to the top in ascending order, because an empty string sorts before everything. Remove them first unless you want them there.
- A header row sorts with the data. Paste a list with a heading line and the heading ends up somewhere in the middle. Take it off first and put it back afterwards.
Reverse is not the opposite of natural
Reversing a natural sort gives you natural order descending, which is what you want. Reversing the lines of an already-sorted list also gives descending order for simple data and does not for anything where the sort was doing real work.
Use the descending option rather than sorting and then reversing.
What this is for
- Tidying a list of names, tags, keywords or references into a readable order.
- Finding duplicates in a list by grouping them together.
- Preparing two lists for comparison — sorting both first means a diff shows genuine differences rather than ordering differences.
- Making a long list scannable, which is the everyday case and the reason it exists.
Nothing is uploaded; the sort happens in your browser.