Reputation: 271
I'm currently have a larger program in LuaTeX (which is a TeX engine with a builtin lua interpreter) and in one part of the program a table is sorted. The table elements are itself tables with a certain structure and the sorting function looks like this:
function sort_list_function (a,b)
if a.totalfilll < b.totalfilll then
return true
elseif a.totalfill < b.totalfill then
return true
elseif a.totalfil < b.totalfil then
return true
elseif a.totalvalue + a.height + a.totalplus <
b.totalvalue + b.height + b.totalplus
then
return true
else
return false
end
end
All element values are numbers, so from my understanding the requirement for the comparison function are fulfilled, but perhaps my thinking is off here (which is basically the question, i.e., why or under what circumstances could the above result in an invalid order function error).
The error is unfortunately only very diffcult to isolate and happened only in once case and then only after the codes has done very many sorts successfully, so as a first step I want to make sure that I'm not totally missing something clearly wrong with a function like the above.
Upvotes: 9
Views: 8152
Reputation: 61
An alternative method of comparison that I find easy to understand.
function sort_list_function (a,b)
if a.totalfilll ~= b.totalfilll then
return a.totalfilll < b.totalfilll
elseif a.totalfill ~= b.totalfill then
return a.totalfill < b.totalfill
elseif a.totalfil ~= b.totalfil then
return a.totalfil < b.totalfil
else
return ( a.totalvalue + a.height + a.totalplus <
b.totalvalue + b.height + b.totalplus )
end
end
Upvotes: 1
Reputation: 271
ok, thanks to the hint from @ColonelThirtyTwo, the answer is that the comparison function is indeed wrong and I have to explicitly handle the >
cases as well and immediately return false (as in my case the different tests are meant to be of different order of importance) e.g., something like
if a.totalfilll < b.totalfilll then
return true
elseif a.totalfilll > b.totalfilll then
return false
elseif a.totalfill < b.totalfill then
return true
elseif a.totalfill > b.totalfill then
return false
elseif a.totalfil < b.totalfil then
return true
elseif a.totalfil > b.totalfil then
return false
else
return ( a.totalvalue + a.height + a.totalplus <
b.totalvalue + b.height + b.totalplus )
end
Upvotes: 8