Lua表排序要求无效的排序功能

Fra*_*ach 5 sorting lua

我目前在LuaTeX中有一个更大的程序(这是一个带有内置lua解释器的TeX引擎),并且在程序的一部分中对表格进行了排序。table元素本身就是具有一定结构的表,排序功能如下所示:

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
Run Code Online (Sandbox Code Playgroud)

所有元素值都是数字,因此据我了解,比较函数的要求已得到满足,但也许我的想法不在这里(这基本上是个问题,即,为什么或在何种情况下上述情况会导致无效的阶次函数错误)。

不幸的是,错误很难被隔离,并且仅在一次成功的情况下才发生,然后在代码成功完成了很多次之后才发生,因此,第一步,我要确保我不会完全遗漏明显的错误。像上面的功能。

Fra*_*ach 5

好的,感谢@ColonelThirtyTwo的提示,答案是比较函数确实是错误的,因此我也必须明确处理这些>情况并立即返回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   
Run Code Online (Sandbox Code Playgroud)