Sorting Arrays
J. Landman Gay
jacque at hyperactivesw.com
Fri Aug 11 18:00:22 EDT 2023
On 8/10/23 2:29 PM, Alex Tweedly via use-livecode wrote:
> Combining my "traditional" way as above, and your example, I came up with a simpler way to do
> the same thing:
>
>> on simpleSortNumberedArray @pArrayDataA, pSortKeys
>> local tKeys, tSeq, tOneSortKey, tSortCommand
>> put seqAsLines(pArrayDataA) into tKeys
>> repeat with I = the number of items in pSortKeys down to 1
>> put item I of pSortKeys into tOneSortKey
>> put "sort lines of tKeys" && word 2 to -1 of tOneSortKey && \
>> "by pArrayData[each][" && word 1 of tOneSortKey && "]" into tSortCommand
>> do tSortCommand
>> end repeat
>> rebuildSeq pArrayDataA, tKeys
>> end simpleSortNumberedArray
>>
>> function seqAsLines pSeq
>> local tRes
>> repeat with i = 1 to the number of elements in pSeq
>> put i & CR after tRes
>> end repeat
>> return tRes
>> end seqAsLines
>>
>> command rebuildSeq @pSeq, pList
>> local tResQ, tCount
>> repeat for each line L in pList
>> add 1 to tCount
>> put pSeq[L] into tResQ[tCount]
>> end repeat
>> put tResQ into pSeq
>> end rebuildSeq
I couldn't get this to work until I altered it, but I was using a very simple array. What type
of array data did you use? I think I'm missing something.
I just did this:
put the weekdayNames into tDataA
split tDataA by cr
simpleSortNumberedArray tDataA, "descending,text"
For that array, this worked:
on simpleSortNumberedArray pArrayDataA, pSortKeys
local tKeys, tSeq, tOneSortKey, tSortCommand
put seqAsLines(pArrayDataA) into tKeys
repeat with I = the number of items in pSortKeys down to 1
put item I of pSortKeys into tOneSortKey
-- put "sort lines of tKeys" && word 2 to -1 of tOneSortKey && \
-- "by pArrayDataA[each][" && word 1 of tOneSortKey && "]" into tSortCommand
put "sort lines of tKeys" && tOneSortKey into tSortCommand
do tSortCommand
end repeat
rebuildSeq pArrayDataA, tKeys
end simpleSortNumberedArray
function seqAsLines pSeq
local tRes
-- repeat with i = 1 to the number of elements in pSeq
repeat for each element e in pSeq
put e & CR after tRes
end repeat
return tRes
end seqAsLines
command rebuildSeq @pSeq, pList
local tResQ, tCount
repeat for each line L in pList
add 1 to tCount
-- put pSeq[L] into tResQ[tCount]
put L into tResQ[tCount]
end repeat
put tResQ into pSeq
end rebuildSeq
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list