我正在尝试从一个文件夹中对创建的文件名列表进行排序。这是代码,因为它是最简单的形式。如果我跑这个,10总是在1而不是9之后。我在看什么。
set composer_list to {"Filename_1", "Filename_2", "Filename_3", "Filename_4", "Filename_5", "Filename_6", "Filename_7", "Filename_8", "Filename_9", "Filename_10", "Filename_11"}
simple_sort(composer_list)
--======================================= Sorting Handler =====================================
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
结果:
{"Filename_1", "Filename_10", "Filename_11", "Filename_2", "Filename_3", "Filename_4", "Filename_5", "Filename_6", "Filename_7", "Filename_8", "Filename_9"}
答案 0 :(得分:4)
使用:
considering numeric strings
simple_sort(composer_list)
end considering
结果:
{"Filename_1", "Filename_2", ..., "Filename_9", "Filename_10", "Filename_11"}
答案 1 :(得分:1)
这是因为
"Filename_11" comes before "Filename_2" -- true
如果您填充列表,它应该可以正常工作。
"Filename_11" comes before "Filename_02" -- false
您应该download Nigel Garvey's "A Dose of Sorts"获得最佳排序程序。
答案 2 :(得分:1)
然而,我遇到过这个问题的一个变种:
我有一个带有带连字符的部分和子部分的列表,使用以连字符分隔的数字(section1,section1-3,section1-3-5,section2-0)。使用原始的simple_sort,1-3-5在1-3之前进入。但是,使用“考虑数字字符串”而不是将连字符视为减号,事情都是混乱的。但是,我添加了另一个子程序来预处理比较的字符串,方法是在比较之前删除超出部分:
on removeHyphens(theText)
set AppleScript's text item delimiters to "-"
set theReturn to every text item of theText
set AppleScript's text item delimiters to ""
set theReturn to theReturn as string
return theReturn
end removeHyphens
然后在simple_sort函数中,我将一行更改为: 否则如果removeHyphens(this_item)出现在removeHyphens(low_item)之前,那么
这对于这种特殊情况来说就像是一种魅力。
答案 3 :(得分:0)
怎么样 忽略连字符 ... 结束无视
但最好的答案是:使用Filename_01(前导0填充)