Most graphical file managers do not sort strictly lexicographically, but rather using “natural sort”. Blocks of numbers in the name are interpreted as numbers - the larger block of numbers wins, even if the opposite would come out in purely alphabetical terms. The idea behind natural sorting: What people mostly want is “9 before 10”, “Chapter 2 before Chapter 10”, without having to add leading zeros.
The following file pairs are naturally sorted in ascending order as follows:
build-9e2.logbuild-950.log
Surprisingly, but explainable: The first digit block \(9\) is smaller than the first digit block \(950\) .
IMG_12113419_90.jpgIMG_0554363070_90.jpg
The number \(12113419\) is less than \(554363070\) (the leading \(0\) is removed).
temp_0C.txttemp_2C.txttemp_-3C.txttemp_10C.txttemp_-12C.txt
The numbers compared are \(0\) , \(2\) , \(3\) , \(10\) , \(12\) – the “-” is not considered part of the number.
Even "alphabetical" isn't globally unambiguous: Capitalization, umlauts like ä (German), or multi-character letters like ch (Czech) lead to legitimate variants. "Purely alphabetical" is therefore context-dependent. Windows Explorer implements this in the StrCmpLogicalW function. While its source code (shlwapi.dll) is proprietary and not public, there are reimplementations, for example, from ReactOS.:
{
TRACE("%s, %s\n", wine_dbgstr_w(str), wine_dbgstr_w(comp));
if (!str || !comp)
return 0;
while (*str)
{
if (!*comp)
return 1;
else if (*str >= '0' && *str <= '9')
{
int str_value, comp_value;
if (*comp < '0' || *comp > '9')
return -1;
/* Compare the numbers */
StrToIntExW(str, 0, &str_value);
StrToIntExW(comp, 0, &comp_value);
if (str_value < comp_value)
return -1;
else if (str_value > comp_value)
return 1;
/* Skip */
while (*str >= '0' && *str <= '9') str++;
while (*comp >= '0' && *comp <= '9') comp++;
}
else if (*comp >= '0' && *comp <= '9')
return 1;
else
{
int diff = ChrCmpIW(*str, *comp);
if (diff > 0)
return 1;
else if (diff < 0)
return -1;
str++;
comp++;
}
}
if (*comp)
return -1;
return 0;
}
Google Drive, OneDrive, KDE, and similar services exhibit similar sorting behavior. CLI tools like... ls and find sort differently than GUI file managers. Semantics are in the filenames, not the API. If you want results without surprises, define conventions: consistent separators, padded numbers and a clear handling of units. Then “alphabetical” becomes predictable again.