Reputation: 19
crdsClear={{y=56,x=50,symbolName=3,},
{y=56,x=29,symbolName=2,},
{y=56,x=99,symbolName=2,},
{y=56,x=9,symbolName=5,},
{y=56,x=69,symbolName=5,},
{y=56,x=19,symbolName=4,},
{y=56,x=59,symbolName=4,},
{y=56,x=89,symbolName=4,},
{y=56,x=40,symbolName=7,},
{y=56,x=80,symbolName=6,},}
tmp2={}
ywf = 1
table.sort(crdsClear,
function(a,b)
tmp2[ywf]=""
for i=1, #crdsClear, 1 do tmp2[ywf] = tmp2[ywf].."\t"..crdsClear[i].x end
ywf = ywf + 1
if a.x <= b.x then print(a.x.." <= "..b.x.." true") else print(a.x.." <= "..b.x.." false") end
return a.x <= b.x -- a.y <= b.y and
end
)
-- Create string
order=""
print(#crdsClear)
result = {[1]=""}
for i=1, #crdsClear, 1 do
order = order..crdsClear[i].x.." "
result[1] = result[1].. crdsClear[i].symbolName
end
print(order)
print(result[1])
I have .x order after sorting:
9 19 59 29 40 50 69 80 89 99
and string:
5442735642
Why i have incorrect order?
If i change:
return a.x <= b.x
to:
return a.x < b.x
then order full correctly.
Upvotes: 0
Views: 231
Reputation: 28950
From the Lua reference manual:
If comp is given, then it must be a function that receives two list elements and returns true when the first element must come before the second in the final order (so that, after the sort, i < j implies not comp(list[j],list[i])).
Using <=
here results in an invalid sort function which in some cases will invoke an error message and/or an incomplete sort result.
Use return a.x < b.x
instead.
Upvotes: 4