Re: table.sort: invalid order function for sorting

'Lars Müller' via lua-l <[email protected]> Fri, 24 Apr 2026 17:28:42 +0200
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
You can easily invert sorting. You just swap the two arguments:

table.sort(Orders, function(a, b) return DateIsLess(b, a) end)

Also not sure I get your point about needing to implement both "less 
than" and "is equal".
"Less than" suffices, you can default "is equal" to "not (lt(a, b) or 
lt(b, a))".

Needing two "comparator" calls to decide equality may of course not be 
ideal for runtime,
but tends to be fine since it usually just adds a constant factor of 
about ~2 at worst.

I think having comparators be boolean "less than" functions makes some 
sense though.
I want to be able to just write something like table.sort(t, 
function(a, b) return a > b end).

Introducing a kind of "spaceship operator" seems like too much for a 
simple scripting language like Lua.
What would the type of the result be? You could make it a number, of 
course, but that's really a hack.
(And one that tends to produce bugs when you have integer wraparound, 
which Lua does.)
I prefer the boolean-typed result of a "less than" comparison.
(I contemplated "lt(a, b)" evaluating to nil if they a == b as a 
convention for comparators,
which would mostly still keep things working as nil is falsey, but 
that's also hacky.)

- Lars

On Fri, Apr 24 2026 at 16:52:27 +02:00:00, 'Martin Eden' via lua-l 
<[email protected]> wrote:
> On 2026-04-24 16:21, Philippe Verdy wrote:
>> Sort algorithms assume that when compariing pairs of items, if this
>> comparison return true, then the two items dont need to be swapped; 
>> if this
>> comparison returns false, the two items are swapped unconditionally.
>> Normally also the two compared temps a and b are where where the 
>> index of
>> item "a" in the table is LOWER than the index of item "b".
>> 
>> Now take the case of a table which is ALREADY COMPLETELY SORTED.
>> - Take the extreme case where all items in the table are equal to 
>> each
>> other: the sort function wll compare items using that function that 
>> returns
>> false for EVERY compared pair, so the algorithm will always swap 
>> items,
>> everywhere. This makes the sort function VERY UNSTABLE
>> - Take another extreme case where all items in the table are NaN 
>> floatting
>> number: the sort function will compare items using that function 
>> that also
>> return false for EVERY compared pair, so the algorithms will also 
>> always
>> swap items.
>> 
>> Lua has used a very uncommon definition of a generic table sort 
>> algorithm
>> which will NEVER be stable, and will be unnecessarily very 
>> slow/inefficient
>> (for tables that are already sorted but contain duplicate values or 
>> NaN
>> values), because its compare function is (a < b) with a strict 
>> comparison,
>> instead of the usual (a <= b) with a non-strict comparison!
>> 
>> This absence of basic stability is that you CANNOT use table sort() 
>> in
>> successive times (but using different column subitems) to perform a
>> multi-keyed sort (where duplicate values are expected, notably for 
>> the
>> primary sort key which is the last key used to sort the table), as
>> secondary (or tertiary) keys that were first sorted will be SHUFFLED 
>> when
>> sorting by the primary key.
>> 
>> This is a severe design bug for table.sort()!
> 
> Nice examples, Philippe
> 
> I've thought about current Lua's approach. Requiring strict ordering
> (with inherited irreflection) makes some sense if we want to minimize
> number of calls of comparison function.
> 
> But for me it's schizophrenic: trying to fit tertiary value into 
> binary.
> For me linear order comparison has three values: less, equal, greater.
> You can't fit them into boolean and build sorting algorithm on that
> without discovered side effects.
> 
> If someone wants to use boolean comparators - he still needs two of 
> them.
> For example is_less() and is_equal(). Of course I won't be happy
> to provide two almost identical blocks of code for them.
> 
> For me requirement of comparison function to return one of three 
> values
> is more natural and acceptable than providing fragile sorting 
> algorithm
> for binary comparator.
> 
> You can't even easily invert sorting like:
> 
> -- Ascending sort
> table.sort(Orders, function(a, b) return DateIsLess(a, b) end)
> 
> -- Descending sort? Nah! Wandering runtime error!
> table.sort(Orders, function(a, b) return not DateIsLess(a, b) end)
> 
> -- 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] 
> <mailto:[email protected]>.
> To view this discussion visit 
> <https://groups.google.com/d/msgid/lua-l/a832ad81-b9e5-47ac-af82-48c666412809%40disroot.org>.

-- 
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/UB80ET.NK3I2IOQR25S3%40gmx.de.