lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I have some data which if I sort it only once, the result is not
correct (i.e. some fields are at wrong places), and if I sort it
twice, it is correct.

On the other hand, for some other data, if I try to sort it twice, on
the second pass the comparator will receive nil for one of its inputs.

I didn't expect to have any nil values, or non-contiguous indices,
since all the tables are loaded exclusively through
table.insert(table, value) (i.e. appended to the end).

I have even tried to rewrite the tables with explicite indices before
sorting like this:

    local newByIndex = {};
    local newI = 0;
    for i, cvar in ipairs(self.byIndex) do
      if cvar ~= nil then
        newI = newI+1;
        newByIndex[newI] = cvar;
      end
    end
    table.setn(newByIndex, newI);
  
    table.sort(newByIndex, compareCvars);
    table.sort(newByIndex, compareCvars);

But I still get nils on the second sort.

This is in Lua 5.0. Is this a known problem that was fixed for 5.1?
Would upgrading to 5.1 help? (I'm not too eager to try that without a
reason, since I expect a lot of problems with changed behavior in
gsub.)

Thanks in advance,
Alen