LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   table.sort problem (https://www.lotrointerface.com/forums/showthread.php?t=3810)

surreal 10-14-2020 03:28 PM

table.sort problem
 
Hi, I'm unsure if this is possible. But basically I want everything with 100 chance to be at the top then sorted by priority. Anything with not 100 chance I want to sort by priority only.

Example table:
_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

What I've tried:
table.sort(_cmds, function(a,b)
if a.chance == 100 and b.chance ~= 100 then
if a.priority < b.priority then
return true
else
return false
end
else
if a.priority < b.priority then
return true
else
return false
end
end
end)

Would like the table to end up looking like this:
_cmds = {
{ chance = 100, priority = 7 },
{ chance = 100, priority = 10 },
{ chance = 50, priority = 1 },
{ chance = 75, priority = 5 },
}

Garan 10-14-2020 10:20 PM

Try
Code:



_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 and b.chance == 100 then
                if a.priority < b.priority then
                        return true
                else
                        return false
                end
        else
                if a.chance==100 and b.chance~=100 then
                        return true
                else
                        if a.chance~=100 and b.chance==100 then
                                return false
                        else
                                if a.priority < b.priority then
                                        return true
                                else
                                        return false
                                end
                        end
                end
        end
end)

Hint, when comparing any two rows, there are four possible conditions for the chance: both equal 100, first equal 100 and second not equal 100, first not equal to 100 and second equal 100 and finally the default of neither equal 100 (you only care about priority in the first and last cases). Note, this could be simplified slightly, but by laying it out this way it is easy to see how the conditionals work out. For instance, the following code has the same result:
Code:

_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 then
                if b.chance==100 then
                        if a.priority < b.priority then
                                return true
                        else
                                return false
                        end
                else
                        return true
                end
        else
                if b.chance==100 then
                        return false
                else
                        if a.priority < b.priority then
                                return true
                        else
                                return false
                        end
                end
        end
end)

A slightly more elegant solution returns the result of the priority comparison without an explicit if-then clause:
Code:

_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 then
                if b.chance==100 then
                        return (a.priority < b.priority)
                else
                        return true
                end
        else
                if b.chance==100 then
                        return false
                else
                        return (a.priority < b.priority)
                end
        end
end)

There are even more elegant but less easily understood versions but you get the idea...

surreal 10-14-2020 11:54 PM

Thank you, it work't perfectly!

Now that I see the code and think about it, it makes sense. I just could'nt figure it out myself :S Hopefully next time I will. Thanks a bunch

Quote:

Originally Posted by Garan (Post 12530)
Try
Code:



_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 and b.chance == 100 then
                if a.priority < b.priority then
                        return true
                else
                        return false
                end
        else
                if a.chance==100 and b.chance~=100 then
                        return true
                else
                        if a.chance~=100 and b.chance==100 then
                                return false
                        else
                                if a.priority < b.priority then
                                        return true
                                else
                                        return false
                                end
                        end
                end
        end
end)

Hint, when comparing any two rows, there are four possible conditions for the chance: both equal 100, first equal 100 and second not equal 100, first not equal to 100 and second equal 100 and finally the default of neither equal 100 (you only care about priority in the first and last cases). Note, this could be simplified slightly, but by laying it out this way it is easy to see how the conditionals work out. For instance, the following code has the same result:
Code:

_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 then
                if b.chance==100 then
                        if a.priority < b.priority then
                                return true
                        else
                                return false
                        end
                else
                        return true
                end
        else
                if b.chance==100 then
                        return false
                else
                        if a.priority < b.priority then
                                return true
                        else
                                return false
                        end
                end
        end
end)

A slightly more elegant solution returns the result of the priority comparison without an explicit if-then clause:
Code:

_cmds = {
{ chance = 50, priority = 1 },
{ chance = 100, priority = 10 },
{ chance = 100, priority = 7 },
{ chance = 75, priority = 5 },
}

table.sort(_cmds, function(a,b)
        if a.chance == 100 then
                if b.chance==100 then
                        return (a.priority < b.priority)
                else
                        return true
                end
        else
                if b.chance==100 then
                        return false
                else
                        return (a.priority < b.priority)
                end
        end
end)

There are even more elegant but less easily understood versions but you get the idea...



All times are GMT -5. The time now is 03:29 AM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI