Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua table sort claims invalid order function for sorting

Tags:

sorting

lua

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.

like image 603
Frank Mittelbach Avatar asked May 07 '16 18:05

Frank Mittelbach


People also ask

How does table sort work Lua?

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.

What sorting algorithm does Lua use?

The table library provides an in-place sort function, based on the quicksort algorithm [wikipedia].


2 Answers

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   
like image 156
Frank Mittelbach Avatar answered Sep 30 '22 07:09

Frank Mittelbach


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
like image 37
user2683482 Avatar answered Sep 30 '22 06:09

user2683482