我目前在 LuaTeX(这是一个带有内置 lua 解释器的 TeX 引擎)中有一个更大的程序,并且在程序的一部分中对表进行了排序。表格元素本身就是具有一定结构的表格,排序功能如下所示:
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
所有元素值都是数字,因此根据我的理解,满足了比较函数的要求,但也许我的想法在这里不正确(这基本上是问题,即为什么或在什么情况下上述会导致无效的顺序函数错误)。
不幸的是,这个错误很难隔离,并且只发生在一次情况下,然后只有在代码成功完成很多种类之后,所以作为第一步,我想确保我没有完全遗漏一些明显错误的功能如上。
最佳答案
好的,感谢@ColonelThirtyTwo 的提示,答案是比较函数确实是错误的,我必须明确处理 >
case 以及立即返回 false(在我的情况下,不同的测试意味着具有不同的重要性)例如,类似
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
关于sorting - Lua table sort 声明无效的排序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37092502/