Page 1 of 1

table.sort

Posted: 16 Sep 2015 00:47
by Barnowl
Has run into the lua table library sort function not being completely reliable?
I have been sorting a list of id / tag pairs, and I wanted the sort to be first by id and then by tag, with the wrinkle that Name would always come first. It works fine except for an occasional few cases where the name did not come first. I debugged the sort function and could not spot it making any mistakes. I then knocked up a bubble sort and used that instead, and no mistakes.
The attached plugin takes one of my errant data sets and sorts it both ways, and you can see the third and fourth items are incorrect with table.sort and correct with the bubble. Counting the calls to the sort function shows up the inefficiency of the bubble - but accuracy is the first priority!

Re: table.sort

Posted: 16 Sep 2015 00:50
by Barnowl
Sorry missed the attachment :?

Re: table.sort

Posted: 16 Sep 2015 10:46
by tatewise
BTW: You could have added the Attachment by using the left most Edit post icon on your original posting.

Yes, it is a bit mysterious, but debugging the sortfunc with a break point where C1.id == 510 reveals that table.sort sometimes compares a table entry with itself.
e.g. C1 = { id=510, tag="NAME" } versus C2 = { id=510, tag="NAME" }

In this case the bRet value must be false, i.e. C1 is not less than C2.

So in the script after
if (C1.id == C2.id) then
insert
if (C1.tag == C2.tag) then
bRet = false
else

with a matching end and it works correctly!
(There may be a slightly more efficient solution.)

P.S.
I added the following two lines to your test data:

t[25] = { id=510, tag="CENS" }
t[26] = { id=510, tag="CENS" }

and with old sortfunc it even mis-sorts the id but the new code is OK and uses less comparisons.

Re: table.sort

Posted: 16 Sep 2015 11:54
by Barnowl
Thanks Mike. Just shows that you have to read the library reference with a magnifying glass!! I had spotted the comparisons of the record with itself, but assumed it wouldn't matter what was returned in that case - which is true for the bubble sort. But as you point out the manual does say return true if less.

Re: table.sort

Posted: 16 Sep 2015 18:00
by tatewise
A couple of tiny tips, that may not be relevant here, but may help later.

Instead of for n = 1, 24 do use for n = 1, #t1 do which automatically adapts to the size of a simple array table.

Instead of t[1] = { id=1398, tag="NAME" } et seq, use table.insert({ id=1398, tag="NAME" }) which automatically increments the index.