How To Sort Lines Of Text
📖 Bu rehber ToolPazar ekibi tarafından hazırlanmıştır. Tüm araçlarımız ücretsiz ve reklamsızdır.
Lexicographic vs alphabetical vs numeric
Sorting a list of lines is a one-click operation until the moment you realize the sort put “Item 10” before “Item 2,” mixed your capitalization wrong, or flipped Spanish ñ into the wrong alphabetical slot. “Sort these lines” hides a dozen decisions: alphabetical or numeric, case-sensitive or not, natural or lexicographic, stable or not, and which locale’s collation rules. This guide walks through each choice, shows where the common mistakes happen, and covers the specialized modes — natural sort for mixed alphanumeric, reverse sort for most-recent-first, and locale-aware sort for non-English text — that handle real-world data correctly.
Case sensitivity
“Item 10” sorts before “Item 2” lexicographically because ‘1’ < ‘2’ in ASCII. Natural sort fixes this.
Natural sort
In ASCII, uppercase letters all have lower code points than lowercase. A case-sensitive sort produces:
Stable sort
Case-insensitive sort groups them together:
Reverse sort
Case-insensitive is almost always what humans want for human-readable lists. Case-sensitive is right when you’re sorting identifiers, code, or anything where case carries meaning.
Locale-aware collation
Natural sort recognizes runs of digits and compares them numerically. It’s what you want for filenames, version numbers, chapter lists, and anything with embedded numbers.
Diacritic handling
A stable sort preserves the relative order of lines that compare equal. Non-stable sort may reshuffle them. This matters when you sort on one key and want a previous ordering preserved as the tiebreaker.
Sorting with a header line
Sort ascending, then reverse. Or pass a descending flag. Most languages have a one-liner:
Sorting by column
Code-point order is not alphabetical order in many languages. A few examples:
Large files and memory
Using lexicographic sort for filenames and getting “file10” before “file2.” Running a case-sensitive sort on mixed-case human names. Sorting German or Spanish lists with default ASCII collation. Reading the sort output as “stable” when you ran a non-stable algorithm. Forgetting that your header line is now somewhere in the middle of the list.