Intelligent sorting: A bit of a poser RESULTS CORRECTION
FlexibleLearning
admin at FlexibleLearning.com
Fri Jul 2 02:19:03 EDT 2010
I made an inexcusable error when applying the solutions in the benchtests.
When adjusting Mike's solution to handle commas in the list I omitted to
adjust the itemDel. The corrected solution is below.
As a result, Mike's solution is not only very fast, but also sub-sorts the
alpha component and handles mixed suffix components (which is very cool).
However, as pointed out, it cannot handle alpha-only list items. Dave's
solution can handle lists with or without numbers, but the sort order is
inexact.
Using the insights of both solutions, I have based a composite solution on
Mike's routine adjusted with the flexibility Dave's routine. It has Mike's
speed and ability to sort mixed suffixes, but includes Dave's ability to
sort mixed alpha-only and alphanumeric lists. I think this provides the best
of everything for a generic library function...
function sortMe5 pList
--| Hugh Senior <admin at FlexibleLearning.com>
--| Based on a solution by Mike Bonner <bonnmike at gmail.com>
set the itemDel to numtochar(8)
repeat for each line theLine in pList
if matchchunk(theLine,"([a-zA-Z\s]\d)",theChar,theEnd) then
put numtochar(8) after char theChar of theLine
else put numtochar(8) after theLine
put theLine & CR after tTemp
end repeat
delete last char of tTemp
sort lines of tTemp numeric by item 2 of each
sort lines of tTemp by item 1 of each
replace numtochar(8) with "" in tTemp
return tTemp
end sortMe5
a 1
b20
a 20
a 2
b10
a 3
b3
a 1a
b2
a 10
b1a
d
c
b
a
gives...
a
a 1
a 1a
a 2
a 3
a 10
a 20
b
b1a
b2
b3
b10
b20
c
d
Prior Work...
function sortMe1 pVar
--| Mike Bonner <bonnmike at gmail.com>
set the itemDel to numtochar(8)
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
/H
More information about the use-livecode
mailing list