Re: table.sort: invalid order function for sorting

"'Martin Eden' via lua-l" <[email protected]> Mon, 27 Apr 2026 00:33:11 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
On 2026-04-26 21:32, 'Lars Müller' via lua-l wrote:
> How so? There is no meaningful difference between "a < b" and "a <= b" 
> as convention for a comparator,
> because "a <= b" is equivalent to "not (b < a)". It is just a 
> convention, and a pretty reasonable one at that.
>
> - Lars 
That's the original issue of this thread.

>   Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
>   > table.sort({ 1, 2, 3, 1 }, function(a, b) return not (a > b) end)
>   stdin:1: invalid order function for sorting
>   stack traceback:
>     [C]: in function 'table.sort'
>     stdin:1: in main chunk
>     [C]: in ?

You can't have (a <= b) comparator in table.sort().

>
> On Sun, Apr 26 2026 at 13:18:38 +02:00:00, Philippe Verdy 
> <[email protected]> wrote:
>> If you want stability, suitable for mitlkey sort, or sorts in 
>> multiple passes, and the minimum of swaps for duplicate values, or 
>> for NaN values), using the comparator (a<b) does not work at all, but 
>> (a<=b) does.
>>
>> Lua made the worst decision for its binary comparator used in 
>> table.sort!
Many recursive sorts are not stable. So you have to use multi-tier 
comparator
because you can't afford multiple passes as in BucketSort. I am more 
saddened
about random run-time explosive effect of this error. Your program may
pass your tests but still explode in a wild.

-- Martin

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/90534326-894a-4993-97b4-b6d4ad80e291%40disroot.org.