Re: table.sort: invalid order function for sorting
Spar <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <1cfb3c64-adef-43de-8915-bf9112a075d3@Spark> |
This is not strict weak order because it lacks irreflexivity. a < a returns false, but yours returns true. Table.sort doesn't compare the same object with itself, but it can definitely compare two identical values in the array if they are present multiple times On Apr 17, 2026 at 15:55 +0300, 'Martin Eden' via lua-l <[email protected]>, wrote: > Hello guys, > > I've just found unobvious thing about table.sort: > > -- ! invalid order function for sorting > table.sort({ 1, 2, 3, 1 }, function(a, b) return (a <= b) end) > > Lua manual says: > > The comp function must define a consistent order; more formally, the > function must define a strict weak order. (A weak order is similar to > a total order, but it can equate different elements for comparison > purposes.) > > and for me <= operator is weak order. > > > Practically it means that table.sort() never passes same item > to comparator and when writing comparisons explicitly, > default is "false": > > local compare = > function(a, b) > if (a < b) then return true end > if (a > b) then return false end > > return false > end > > > This error is not stable for my use case with comparing tables. > "return false" default eliminates it but sometimes "return true" > works too. > > Bug? > > > -- 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/990cd9ac-fdb2-4d05-acac-128559523984%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/1cfb3c64-adef-43de-8915-bf9112a075d3%40Spark.