Sorting Arrays

Bob Sneidar bobsneidar at iotecdigital.com
Fri Aug 11 20:07:47 EDT 2023


Send me what you have. Thanks. 

Sent from my iPhone

> On Aug 11, 2023, at 17:02, Alex Tweedly via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> 
>> On 11/08/2023 23:00, J. Landman Gay via use-livecode wrote:
>> On 8/10/23 2:29 PM, Alex Tweedly via use-livecode wrote:
>> [ ... code from earlier posting ...]
> 
>> 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"
>> 
> What you're missing is that this (simpleSortNumberedArray) is only intended for "numbered array"s (which LC is calling "sequences" in some places); i.e. an array where the (top-level) keys are all consecutive integers, from 1 .... n
> 
> Also, the pSortkeys should be a number of comma-separated items, each of which consists of a key by which you want to sort the array followed optionally by an order and type.
> 
> So you might do something like :
> 
>> on mouseup
>>    local tCounter, tDataA
>> 
>>    repeat for each line L in the weekdayNames
>>       add 1 to tCounter
>>       put L into tDataA[tCounter]["dayname"]
>>       put the number of chars in L into tDataA[tCounter]["charcount"]
>>    end repeat
>> 
>>    -- sorts ascending by name (i.e. F, M, Sa, Su, Th, Tu, W)
>>    simpleSortNumberedArray tDataA, "dayname"
>>    repeat with I = 1 to 7
>>       put tDataA[I]["charcount"] && tDataA[I]["dayname"] & CR after msg
>>    end repeat
>> 
>>    put "---------" &CR after msg
>> 
>>    -- sorts descending numeric by number of characters in name
>>    --  NB within each char count value, they remain in alphabetical order of name
>>    simpleSortNumberedArray tDataA, "charcount numeric descending"
>>    repeat with I = 1 to 7
>>       put tDataA[I]["charcount"] && tDataA[I]["dayname"] & CR after msg
>>    end repeat
>> end mouseup
> and get as output
> 
>> 6 Friday
>> 6 Monday
>> 8 Saturday
>> 6 Sunday
>> 8 Thursday
>> 7 Tuesday
>> 9 Wednesday
>> ---------
>> 9 Wednesday
>> 8 Saturday
>> 8 Thursday
>> 7 Tuesday
>> 6 Friday
>> 6 Monday
>> 6 Sunday
> 
> So - it would be worth adding a check that the array passed in is indeed a sequence, at the start of simpleSortNumberedArray:
> 
> if NOT (pArrayDataA is an array AND \
>            item 2 of extents(pArrayDataA) is the number of elements in pArrayDataA) then \
>                  return pArrayData
> 
> I'm now going to add this to my personal library, but I'll rename it to
> 
>     seqSortMultipleKeys
> 
> Alex.
> 
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


More information about the use-livecode mailing list