Sort a list of object identifiers: Difference between revisions

Content added Content deleted
m (added Category:Sorting)
m (→‎{{header|AppleScript}}: Minor change to sort in vanilla version.)
Line 116: Line 116:
*)
*)


on ShellSort(theList)
on ShellSort(theList, l, r)
script o
script o
property lst : theList
property lst : theList
Line 123: Line 123:
set listLength to (count theList)
set listLength to (count theList)
if (listLength > 1) then
if (listLength > 1) then
-- Convert negative and/or transposed range indices.
set stepSize to listLength div 2
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
-- Do the sort.
set stepSize to (r - l + 1) div 2
repeat while (stepSize > 0)
repeat while (stepSize > 0)
repeat with i from (1 + stepSize) to listLength
repeat with i from (l + stepSize) to r
set currentValue to item i of o's lst
set currentValue to item i of o's lst
repeat with j from (i - stepSize) to 1 by -stepSize
repeat with j from (i - stepSize) to l by -stepSize
set thisValue to item j of o's lst
set thisValue to item j of o's lst
if (currentValue < thisValue) then
if (currentValue < thisValue) then
Line 142: Line 148:
end if
end if
return -- nothing.
return -- nothing. The input list has been sorted in place.
end ShellSort
end ShellSort
property sort : ShellSort


-- Test code: sort items 1 thru -1 (ie. all) of a list of strings, treating numeric portions numerically.
-- Test code:
set theList to {"1.3.6.1.4.1.11.2.17.19.3.4.0.10", "1.3.6.1.4.1.11.2.17.5.2.0.79", "1.3.6.1.4.1.11.2.17.19.3.4.0.4", ¬
set theList to {"1.3.6.1.4.1.11.2.17.19.3.4.0.10", "1.3.6.1.4.1.11.2.17.5.2.0.79", "1.3.6.1.4.1.11.2.17.19.3.4.0.4", ¬
"1.3.6.1.4.1.11150.3.4.0.1", "1.3.6.1.4.1.11.2.17.19.3.4.0.1", "1.3.6.1.4.1.11150.3.4.0"}
"1.3.6.1.4.1.11150.3.4.0.1", "1.3.6.1.4.1.11.2.17.19.3.4.0.1", "1.3.6.1.4.1.11150.3.4.0"}
considering numeric strings
considering numeric strings
ShellSort(theList)
sort(theList, 1, -1)
end considering
end considering
return theList</lang>
return theList</lang>