When I first learned about text sorting in programming, I was surprised by how much complexity hides behind that simple "A-Z" button. Let me explain what's really happening when you sort text, and why sometimes it doesn't work quite how you expect.
The Character-by-Character Comparison
Sorting doesn't look at words as whole units—it compares them character by character, like how you'd look up words in a dictionary. Take "apple" and "application". The sorter compares 'a' to 'a' (equal), 'p' to 'p' (equal), 'p' to 'p' (equal), 'l' to 'l' (equal), then 'e' to 'i'. Since 'e' comes before 'i', "apple" comes first.
"apple" vs "application"
1. a = a (continue)
2. p = p (continue)
3. p = p (continue)
4. l = l (continue)
5. e vs i → e comes first
Result: "apple" before "application"
Case Sensitivity: The Hidden Variable
This is where many people get tripped up. Computers don't see 'A' and 'a' as the same letter—they see different character codes. 'A' is 65, 'a' is 97 in ASCII. So with case-sensitive sorting, all uppercase comes before lowercase. That's why "Apple" sorts before "apple" when case-sensitive is on.
"Apple"
"BANANA"
"banana"
All treated as lowercase
"Apple" (uppercase A)
"apple" (lowercase)
"banana" (lowercase)
ASCII order: A-Z then a-z
The Empty Line Dilemma
Empty lines are tricky. They're not "nothing"—they're a line break character. When sorting, an empty line compares as zero characters. Since nothing comes before something, empty lines naturally rise to the top. That's why we have the "remove empty lines" option—it cleans up your results by filtering those out before sorting.