Intelligent sorting: A bit of a poser RESULTS

Mike Bonner bonnmike at
Thu Jul 1 15:30:59 EDT 2010

For strictly numeric, remove this line.

sort lines of tTemp ascending by item 1 of each

For some reason I got it in my head that you wanted the alphas
grouped, then each alpha group by numeric.

As pointed out, my solution didn't allow for commas in the list, and
also didn't allow for names with additional separate numerics IO

Daves solution is really nice since it matches everything up to the
last group of numbers before doing the split. It also handles lines
with no numbers fine which is cool, but I think the sort order ends up
being the same?  Sorts by numeric first, then by line to group
together similar entries.. Would have to remove the same line from
Dave's script for strictly numeric sort also. Same for Jan's, remove
the extra sort and it's straight numeric.

On Thu, Jul 1, 2010 at 12:50 PM, FlexibleLearning
<admin at> wrote:
> 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>
>  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>
>  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>
>  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>
>  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
> _______________________________________________
> use-revolution mailing list
> use-revolution at
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:

More information about the Use-livecode mailing list