Intelligent sorting: A bit of a poser RESULTS
FlexibleLearning
admin at FlexibleLearning.com
Thu Jul 1 14:50:25 EDT 2010
Oh boy! Just shows what a great list this is, and how a challenge sparks
invention! Thanks to ALL who contributed and participated from which golden
nuggets can be gleaned by everyone.
The following 4 solutions were benchtested against a list of 10,000 lines
using a fixed string suffixed with a random number 1-10,000. Each solution
had to correctly sort the list by the suffix number, and the time was
averaged over 100 iterations.
Results:
- Mike Bonner: 107ms, but sort order not strictly numeric.
- Dave Cragg: 142ms. Correct sort order.
- Craig: Correct sort order. Fine for short lists but benchtest was aborted.
- Jan Schenkel: 100ms, but sort order not strictly numeric.
Both Mike and Jan's solutions came up with the same result in essentially
the same time, but as the sort order produced is not strictly numeric the
solutions have to be disqualified. Craig's solution correctly produces a
numerically sequenced list and works well on short lists where 'for each' is
not required, but fails on long lists due to time. The winner, therefore, is
Dave Cragg whose solution produced the correct result and handles long
lists. Dave's solution below has been edited by setting an itemDel in order
to support commas in lists ; otherwise all his own work!
The solutions:
function sortMe1 pVar
--| Mike Bonner <bonnmike at gmail.com>
repeat for each line theLIne in PVar
get matchchunk(theLine,"([a-zA-Z\s]\d)" , theChar,theEnd )
put numtochar(8) after char theChar of theLine
put theLine & return after tTemp
end repeat
delete the last char of tTemp
sort lines of tTemp ascending numeric by item 2 of each
sort lines of tTemp ascending by item 1 of each
replace numtochar(8) with empty in tTemp
return tTemp
end sortMe1
function sortMe2 tData
--| Dave Cragg <dave.cragg at lacscentre.co.uk>
set the itemDel to numtochar(8)
put "(^.*?)([0-9]*$)" into tRE
put "" into tData2
repeat for each line tLine in tData
get matchText(tLine, tRE, tS, tNum)
put tS & numtochar(8) & tNum & cr after tData2
end repeat
sort lines of tData2 numeric by item -1 of each
sort lines of tData2 by item 1 of each
put "" into tData3
repeat for each line tLine in tData2
put item 1 to -2 of tLine & item -1 of tLine & cr after tData3
end repeat
return tData3
end sortMe2
function sortMe3 var
--| Craig <DunbarX at aol.com>
repeat with y = 1 to the number of lines of var
repeat with u = 1 to the number of chars of line y of var
if char u of line y of var is n "0123456789" then
put comma before char u of line y of var
exit repeat
end if
end repeat
end repeat
sort var numeric by item 2 of each & item 1 of each
sort var by item 1 of each
replace comma with empty in var
return var
end sortMe3
function sortMe4 tText
--| Jan Schenkel <janschenkel at yahoo.com>
sort lines of tText numeric by TrailingNumber(each)
sort lines of tText
return tText
end sortMe4
private function TrailingNumber pLine
local tNumber, tChar
repeat with tIndex = length(pLine) to 1 step -1
put char tIndex of pLine into tChar
if tChar is not an integer then exit repeat
put tChar before tNumber
end repeat
if tNumber is empty then put 0 into tNumber
return tNumber
end TrailingNumberend sortMe3
/H
aka Hugh Senior, FLCo
More information about the use-livecode
mailing list