自然排序顺序混乱

大多数图形文件管理器并非严格按照字典顺序排序,而是使用“自然”排序。名称中的数字块会被解释为数字——较大的数字块优先,即使字母顺序相反也是如此。自然排序背后的理念是:人们通常希望的是“9 在 10 之前”、“第 2 章在 10 之前”——而无需添加前导零。


以下文件对按自然升序排列如下:

  • build-9e2.log
  • build-950.log

令人惊奇,但可以解释:第一个数字\(9\)小于第一个数字块\(950\)

  • IMG_12113419_90.jpg
  • IMG_0554363070_90.jpg

数字\(12113419\)小于\(554363070\) (前导\(0\)被删除)。

  • temp_0C.txt
  • temp_2C.txt
  • temp_-3C.txt
  • temp_10C.txt
  • temp_-12C.txt

比较的数字是\(0\) , \(2\) , \(3\) , \(10\) , \(12\) – “-” 不被视为数字的一部分。

即使是“字母顺序”也并非全局无歧义:大写字母、变音符号(例如 ä(德语))或多字符字母(例如 ch(捷克语))都会导致合法的变体。因此,“纯字母顺序”则依赖于上下文。Windows 资源管理器在StrCmpLogicalW函数中实现了此功能。虽然其源代码(shlwapi.dll)是专有的且不公开,但有一些重新实现,例如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 和其他工具也表现出类似的排序行为。CLI 工具,例如 lsfind 但是,它们的排序方式与 GUI 文件管理器不同。语义体现在文件名中,而不是 API 中。如果您希望结果不令人意外,请定义约定:一致的分隔符、填充的数字以及清晰的单位处理。这样,“字母顺序”就又变得可预测了。

背部