Intelligent sorting: A bit of a poser NEW WINNER

FlexibleLearning admin at FlexibleLearning.com
Fri Jul 2 15:42:04 EDT 2010


Hi Mike, and welcome to the party!

Your solution is pretty darn good. Fast, too, with 53 m/s avg benchtime (100
iterations of a 10,000 random list) so by far the quickest (closest is 74
m/s by Mike Bonner). It successfully sorts the sample data suplied, a feat
achieved only by Craig.

On the highly random benchtest data it is also fairly accurate, but not
100%. But then again, no-one has achieved that yet, possibly because the
degree of randomness was not specified.

On the basis of the posted 'challenge', Mike... Looks like you are the new
winner!

Well done that man.

/H


From: Michael Kann <mikekann at yahoo.com>

Hugh, thanks for providing a fun and educational challenge -- and organizing
the results. I woke up with an idea. Can you time my late entry?
Thanks.

Mike

-------------------------------------
-- I think the script is pretty
-- self-explanatory
-------------------------------------


on mouseUp
put fld 1 into v -- original data
set the itemDel to "*"

repeat with k = 0 to 9
  replace k with "*"&k in v
end repeat

repeat for each line k in v
  replace "*" with "" in item 2 to -1 of k
  put k & cr after h
end repeat
delete last char of h

sort h numeric by item 2 of each
sort h by item 1 of each
replace "*" with "" in h

put h into fld 2 -- output
end mouseUp

-------------------------------------

--- On Fri, 7/2/10, FlexibleLearning <admin at FlexibleLearning.com> wrote:

> From: FlexibleLearning <admin at FlexibleLearning.com>
> Subject: Re: Intelligent sorting: A bit of a poser RESULTS CORRECTION
> To: use-revolution at lists.runrev.com
> Date: Friday, July 2, 2010, 1:19 AM
> 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