table.sort: invalid order function for sorting

"'Martin Eden' via lua-l" <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.