sorting words ?
Kay C Lan
lan.kc.macmail at gmail.com
Mon Dec 7 19:41:14 EST 2015
On Tue, Dec 8, 2015 at 6:17 AM, <jbv at souslelogo.com> wrote:
> but I actually have multiple lines with multiple items in each,
> and multiple words in each item... so working with arrays will do
> the sorting trick.
>
Yes it will but for those who are array adverse here's a way to do it using
'itemDel' and another using 'replace'. To test which was quicker (itemDel)
I repeated it 10,000 times. Put this in the msg box:
put "99 35 82,215 83 44,75 16 97,487 89 30,81 42 03" into line 1 of
myListOfWordsInItemsInLines
put "23 04 35,96 27 67,38 89 10,41 92 13,74 35 06" into line 2 of
myListOfWordsInItemsInLines
put "87 18 69,50 38 97,96 45 74,33 82 11,62 43 14" into line 3 of
myListOfWordsInItemsInLines
put "5 034 123,9 256 51,33 745 56,767 27 97,17 002 223" into line 4 of
myListOfWordsInItemsInLines
put 10000 into tRepeats
put the millisec into tStartTime
repeat tRepeats times
put empty into tOutput
--SORTING WORDS USING REPLACE
repeat for each line myLine in myListOfWordsInItemsInLines
repeat for each item myItem in myLine
replace space with comma in myItem
sort items of myItem ascending numeric
--If you only have single digit numbers as per your example you
don't need 'ascending numeric'
replace comma with space in myItem
put myItem & comma after tLine
end repeat
put char 1 to -2 of tLine & cr after tOutput
put empty into tLine
end repeat
end repeat
--tOutput contains the sorted words
put the millisec into tEndTime
put tRepeats & " repeats using 'replace' took " & (tEndTime -
tStartTime) & "ms" & cr into msg
put the millisec into tStartTime
repeat tRepeats times
put empty into tOutput2
--SORTING WORDS USING ITEMDEL
repeat for each line myLine in myListOfWordsInItemsInLines
repeat for each item myItem in myLine
set the itemDel to space
sort items of myItem ascending numeric
--If you only have single digit numbers as per your example you
don't need 'ascending numeric'
put myItem & comma after tLine
set the itemDel to comma
end repeat
put char 1 to -2 of tLine & cr after tOutput2
put empty into tLine
end repeat
end repeat
--tOutput2 contains the sorted words
put the millisec into tEndTime
put tRepeats & " repeats using 'itemDel' took " & (tEndTime -
tStartTime) & "ms" & cr after msg
if (tOutput = tOutput2) then
put "Both produce the same result" & cr & cr after msg
put tOutput after msg
else
put "Error! The outputs do NOT match." & cr & cr after msg
put tOutput & cr & cr after msg
put tOutput2 after msg
end if
More information about the use-livecode
mailing list