lsort 不能正确地正确排序文件名

lsort doesn't properly sort file names properly

我像这样在存储库文件夹中捕获图片列表:

foreach image [lsort [glob -nocomplain -directory $image_path -type f *]] {
    puts $image
}

由于lsort,所有图片返回时都已排序,但有几张图片根本没有排序,我一直无法弄清楚原因。

从文件夹返回的顺序是:

Repository/Unsorted/3.jpg
Repository/Unsorted/30.jpg
Repository/Unsorted/33.jpg
Repository/Unsorted/6.jpg
Repository/Unsorted/9.jpg 

预期:

Repository/Unsorted/3.jpg
Repository/Unsorted/6.jpg
Repository/Unsorted/9.jpg 
Repository/Unsorted/30.jpg
Repository/Unsorted/33.jpg

更新: 当我使用 -dictionary 开关时,它 returns 的正确顺序。有人可以详细说明原因吗?

foreach image [lsort -dictionary [glob -nocomplain -directory $image_path -type f *]] {
    puts $image
}

lsort 命令有几种决定元素顺序的方法。

  • -ascii(各种原因的默认模式,略有误名)只是使用了两个字符串中每一对字符的数字排序,按照字符串中字符的顺序。这是您对 C strcmp() 所期望的那种事情……如果它是 Unicode 感知的。

  • -dictionary 是一样的……除了字符串中的数字序列(所以只有字符 09 不是 - 所以没有负数)作为数字进行比较。这给出的排序感觉更像你在字典中得到的东西;它专门用于在 Tk 的文件选择对话框中生成令人满意的文件名顺序。

  • 并且,为了完整起见,-integer-real 分别将字符串解析为整数和浮点数,然后对它们进行排序,而 -command 让您提供您自己的排序命令(较慢,但完全由您控制)。

Tcl 知道以何种顺序对值进行排序的唯一原因是因为您告诉它。

因此,当比较 Repository/Unsorted/3.jpgRepository/Unsorted/30.jpg 时,在 -ascii 模式下 . (Unicode U+00002E) 在 0 (Unicode U +000030),而在 -dictionary 模式下,330 数字序列被解析为整数并以这种方式进行比较(因为之前的非数字部分是相同的)。