可能有点复杂,但您也可以使用Sort
for Table
s 命令来模拟对Strings
. 我已经用几个测试用例进行了尝试,它似乎做得很好(对于黑客来说)。
我添加了对多个Strings
和不区分大小写排序的选项的支持。选项的默认值是需要最少工作量的选项。这是脚本:
form Generic sort...
boolean Numeric_first yes
boolean Case_sensitive yes
endform
n = numberOfSelected("Strings")
for i to n
strings[i] = selected("Strings", i)
endfor
for i to n
# stopwatch
selectObject(strings[i])
nstrings = Get number of strings
# Create an empty table
cols$ = "num str"
if !case_sensitive
cols$ = cols$ + " lc"
endif
table = Create Table with column names: "nums", nstrings, cols$
# Populate the table with the strings or their number versions where possible
for row to nstrings
selectObject(strings[i])
s$ = Get string: row
s = number(s$)
selectObject(table)
Set string value: row, "str", s$
if !case_sensitive
Set string value: row, "lc", replace_regex$(s$, "(.*)", "\L\1", 0)
endif
Set numeric value: row, "num", number(s$)
endfor
sort$ = "num " + if case_sensitive then "str" else "lc" fi
Sort rows: sort$
# Invert order for non-numeric strings first
if !numeric_first
selectObject(table)
nantable = nowarn Extract rows where column (text):
..."num", "is equal to", "--undefined--"
selectObject(table)
numtable = nowarn Extract rows where column (text):
..."num", "is not equal to", "--undefined--"
removeObject(table)
selectObject(nantable, numtable)
table = Append
removeObject(nantable, numtable)
endif
# Replace the original strings with the sorted list
selectObject(table)
for row to nstrings
selectObject(table)
s$ = Get value: row, "str"
selectObject(strings[i])
Set string: row, s$
endfor
# Clean-up
removeObject(table)
# selectObject(strings[i])
# name$ = selected$("Strings")
# time = stopwatch
# appendInfoLine("Sorted ", name$ , " in ", time)
endfor
# Restore selection
if n >= 1
selectObject(strings[1])
for i from 2 to n
plusObject(strings[i])
endfor
endif
编辑
该脚本的改进版本已包含在strutils
CPrAN 插件中。