我目前正在使用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
所有元素值都是数字,因此根据我的理解,比较函数的要求已经满足了,但也许我的想法有误(这基本上就是问题所在,即为什么或在什么情况下以上内容可能导致无效的排序函数错误)。
遗憾的是,这个错误很难被分离出来,并且只在一个案例中发生过,而且只有在代码成功完成了很多排序之后才会发生。因此,作为第一步,我想确保我没有完全忽略了类似上面的函数中明显错误的东西。
a = {totalfilll = 1, totalfill = 2}; b = {totalfilll = 2, totalfill = 1};
,sort_list_function(a,b)
和sort_list_function(b,a)
的结果。 - Colonel Thirty Two