Sorting by item ... of each and item ... of each

Craig Newman craig at starfirelighting.com
Tue Sep 5 10:29:49 EDT 2023


I may be simply misunderstanding…

Since HC days, one could concatenate sort criteria using the “&” character, and obtain multiple stable sorts in one go. In LC as well, since given, in a field 1:

231

312

123

213

321

132

and in a button somewhere:

on mouseUp

get fld 1

sort it numeric by char 1 of each & char 3 of each

answer it

end mouseUp

We get:

132

123

231

213

321

312



Craig




> On Sep 3, 2023, at 8:29 AM, matthias rebbe via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> Hello Mark,
> 
> thanks for explanation and also for your solution. I will try. 
> 
> Maybe i should get my book "regular expressions"  from  Jeffrey E.F.Friedl from the shelf and should finish reading it, so i could make use of regular expressions  in Livecode more often. 
> 
> Regards,
> Matthias
> 
>> Am 03.09.2023 um 11:49 schrieb Mark Waddingham via use-livecode <use-livecode at lists.runrev.com>:
>> 
>> On 2023-09-03 10:26, panagiotis m via use-livecode wrote:
>>> Hello Matthias,
>>> I do not think that the syntax "sort <container> by sortKey1 and sortKey2"
>>> is supported
>> 
>> Heh technically it 'is' - and does do something but it won't be what you expected...
>> 
>> So the syntax for sort in this case is:
>> 
>>  sort <container> by <expression>
>> 
>> This works by iterating over the elements in <container>, passing each through the expression to generate a sort key list, sorting that list and then reordering the original list.
>> 
>> Expressions can contain the boolean 'and' operator, so:
>> 
>>  sort <container> by X and Y
>> 
>> Means that the sort key is evaluated as 'X and Y' for each element - meaning sort ends up sorting a list of 'true' and 'false' values.
>> 
>> As Panos says, if you want to sort by separate fields with decreasing priority you need to do multiple sorts from least priority to most - this works because 'sort' is stable (if two elements compare the same, then the order of them in the output list is the same as the first).
>> 
>> The alternative is to work out a sort expression which combines the two parts of the element you want to sort so that the sort keys sort in that order. This can be quite tricky, but (for example) - let's say your container has elements of the form:
>> 
>>  <section-number>,<sub-section-number>
>> 
>> So you want things in the 'obvious' sorted order - then you could use:
>> 
>>  sort tSections ascending text by format("%08d%08d", item 1 of each, item 2 of each)
>> 
>> Here the sortkey is defined so that the text sort of the sort keys is that same as sorting first by sub-section number and then by section number.
>> 
>> Warmest Regards,
>> 
>> Mark.
>> 
>> -- 
>> Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
>> LiveCode: Build Amazing Things
>> 
>> _______________________________________________
>> 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
> 
> 
> _______________________________________________
> 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