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.
One of the most used functions in Lua is the sort function which is provided by the Lua library which tables a table as an argument and sorts the values that are present inside the table. The sort function also takes one more argument with the table and that argument is a function which is known as the order function.
The table library provides an in-place sort function, based on the quicksort algorithm [wikipedia].
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With