Sort problem

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Sort problem

Michel Machado
Dears,

  The code below (test.lua) fails:

t = {"a", "b", "a", "a"}

table.sort(t,
  function (a, b)
    assert(a ~= nil, "left var is nil!")
    assert(b ~= nil, "right var is nil!")
    return not (a < b) -- I want an inverted sort!
  end)

for i, v in ipairs(t) do
  print(i, v)
end

  The error:

[michel@michel michel]$ lua test.lua
lua: test.lua:5: left var is nil!
stack traceback:
        [C]: in function `assert'
        test.lua:5: in function <test.lua:4>
        [C]: in function `sort'
        test.lua:3: in main chunk
        [C]: ?

  Did I make some mistake?

[ ]'s
Michel Machado


Reply | Threaded
Open this post in threaded view
|

RE: Sort problem

Virgil Smith
Quick comment/curiosity...

does "return (a > b)" work?

This is different from "return not (a < b)".

-----Original Message-----
From: [hidden email]
[[hidden email] Behalf Of Michel Machado
Sent: Monday, July 26, 2004 11:21 AM
To: [hidden email]
Subject: Sort problem


Dears,

  The code below (test.lua) fails:

t = {"a", "b", "a", "a"}

table.sort(t,
  function (a, b)
    assert(a ~= nil, "left var is nil!")
    assert(b ~= nil, "right var is nil!")
    return not (a < b) -- I want an inverted sort!
  end)

for i, v in ipairs(t) do
  print(i, v)
end

  The error:

[michel@michel michel]$ lua test.lua
lua: test.lua:5: left var is nil!
stack traceback:
        [C]: in function `assert'
        test.lua:5: in function <test.lua:4>
        [C]: in function `sort'
        test.lua:3: in main chunk
        [C]: ?

  Did I make some mistake?

[ ]'s
Michel Machado




Reply | Threaded
Open this post in threaded view
|

Re: Sort problem

Aaron Brown-2
In reply to this post by Michel Machado
Michel Machado wrote:

> table.sort(t,
>   function (a, b)
>     assert(a ~= nil, "left var is nil!")
>     assert(b ~= nil, "right var is nil!")
>     return not (a < b) -- I want an inverted sort!
>   end)

I've done this exact same thing.  The reference manual says:

# If comp is given, then it must be a function that receives
# two table elements, and returns true when the first is
# less than the second (so that not comp(a[i+1],a[i]) will
# be true after the sort).

A not-so-obvious corollary is that the function has to
return false if the two items are equal.  The sort algorithm
depends on this being true and does weird stuff if it's not
true.  Instead use "return a > b" as suggested by Virgil
Smith.

(And don't feel bad -- it took me a couple days to figure
this out when it happened to me.)

-- 
Aaron



Reply | Threaded
Open this post in threaded view
|

Re: Sort problem

William Roper
In reply to this post by Virgil Smith
not (a < b) means that if they are equal, it will return true and lua
needs it to be false if they are equal.
where (a > b) returns false if they are equal.

Thats the only case where they are different.

Example:
a b > !< 
1 1 F  T
1 2 T  T
2 1 F  F

Very interesting interaction that I'm glad I know now :)

Ryan.

On Mon, 26 Jul 2004 12:27:23 -0500, Virgil Smith <[hidden email]> wrote:
> Quick comment/curiosity...
> 
> does "return (a > b)" work?
> 
> This is different from "return not (a < b)".
> 
> 
> 
> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of Michel Machado
> Sent: Monday, July 26, 2004 11:21 AM
> To: [hidden email]
> Subject: Sort problem
> 
> Dears,
> 
>   The code below (test.lua) fails:
> 
> t = {"a", "b", "a", "a"}
> 
> table.sort(t,
>   function (a, b)
>     assert(a ~= nil, "left var is nil!")
>     assert(b ~= nil, "right var is nil!")
>     return not (a < b) -- I want an inverted sort!
>   end)
> 
> for i, v in ipairs(t) do
>   print(i, v)
> end
> 
>   The error:
> 
> [michel@michel michel]$ lua test.lua
> lua: test.lua:5: left var is nil!
> stack traceback:
>         [C]: in function `assert'
>         test.lua:5: in function <test.lua:4>
>         [C]: in function `sort'
>         test.lua:3: in main chunk
>         [C]: ?
> 
>   Did I make some mistake?
> 
> [ ]'s
> Michel Machado
> 
>

Reply | Threaded
Open this post in threaded view
|

Re: Sort problem

Michel Machado
In reply to this post by Aaron Brown-2
Dears,

  I think that this comment could be added to the Lua manual.

  Thanks Aaron!

[ ]'s
Michel Machado

On Mon, 2004-07-26 at 14:45, Aaron Brown wrote:
> Michel Machado wrote:
> 
> > table.sort(t,
> >   function (a, b)
> >     assert(a ~= nil, "left var is nil!")
> >     assert(b ~= nil, "right var is nil!")
> >     return not (a < b) -- I want an inverted sort!
> >   end)
> 
> I've done this exact same thing.  The reference manual says:
> 
> # If comp is given, then it must be a function that receives
> # two table elements, and returns true when the first is
> # less than the second (so that not comp(a[i+1],a[i]) will
> # be true after the sort).
> 
> A not-so-obvious corollary is that the function has to
> return false if the two items are equal.  The sort algorithm
> depends on this being true and does weird stuff if it's not
> true.  Instead use "return a > b" as suggested by Virgil
> Smith.
> 
> (And don't feel bad -- it took me a couple days to figure
> this out when it happened to me.)