Lua表排序要求无效的排序功能 [英] Lua table sort claims invalid order function for sorting

查看:183
本文介绍了Lua表排序要求无效的排序功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

好,感谢@ColonelThirtyTwo的提示,答案是比较功能确实是错误的,我还必须明确处理>情况并立即返回false(在我的情况下,不同的测试具有不同的重要性顺序),例如

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   

这篇关于Lua表排序要求无效的排序功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆