Sort bug and also multilevel sorting

Neville Smythe neville.smythe at
Wed Sep 6 19:57:03 EDT 2023

Ralph: Interesting. Your code works as you present it, sorting the given items by the sortkey function f1 which adds 1 to each item. I do get the “sorting failed” dialog.

But if I try the same thing using myVal modified as you suggest, which adds the first and second items of each line of data and then adds 1 to the global s1, I get the bug I reported: the handler simply exits without showing either the “sorting failed” or the “Done” answer dialog.

I would have thought the two methods would generate the same error code, an incorrect operand in a sum, so this is strange! The function definitely returns on the error line, the value of s1 is not changed in the next line in either your f1 or my myVal. So perhaps the bug occurs when the processing returns to the sort code. Not something we can solve here, one for the engineers to sort out (!)

Mark Waddingham says the sortKey function is intended to be tolerant of errors, simply returning the original value of ‘each’ - in your case each item, in my case each line. So it should either 

1. Continue with the (undocumented) sort order  OR 

2a. exit to top AND 2b. drop into the debugger/errorDialog. In my example it is doing  2a but not 2b.

Craig: The missing bit is that your method won’t work if the integers you are sorting by having different string lengths

The lines

should, when sorted by numeric ascending (item 1 of each, item 2 of each) - which would be the appropriate syntax for a multilevel sort tho not in LC - result in

But if sorted by (item 1 of each & item 2 of each) - which concatenates as strings - gives sort keys 199 and 27 to result in
This can be fixed by formatting the numbers  so they have the same length by padding with 0s, as MW noted. Once they have the same length you could actually sort lexically which may be a lot faster than sorting numerically (although my guess is using 2 separate sorts may be faster still at least for short lists).

The concatenation method also won’t work if sorting real numbers:
since after concatenation the second decimal place would be ignored in a numeric sort.

Neville Smythe

More information about the use-livecode mailing list