I don't know how to handle nils
my sort function gets.
When I have this checking in it, table.sort
crashes after some calls.
if a == nil then
return false
elseif b == nil then
return true
end
With this error:invalid order function for sorting. But according to the documentation, sort function should return false, if a goes after b. True otherwise.
If I remove remove that code, it of course crashes of indexing nils.
This has little or nothing to do with
nil
values in the table. The error message is generated if the comparison function itself is invalid. From the documentation fortable.sort
:In other words,
comp(a,b)
must implynot comp(b,a)
. If this relation does not hold, then the error "invalid order function for sorting" will likely raised. (Note that it might not be raised in all cases.)In order to be more helpful, we really need to see the whole function passed to
table.sort
.To put all nil values at the beginning of the array:
To put all nil values at the end of the array: