table.sort problem - LoTROInterface Search Downloads     #1
 surreal The Wary Join Date: Oct 2020 Posts: 2
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 },
}
#2 Garan The Undying Join Date: Oct 2010 Posts: 283
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...

Last edited by Garan : 10-14-2020 at 10:57 PM. Reason: clarity
#3
 surreal The Wary Join Date: Oct 2020 Posts: 2
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 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...

 Thread Tools Show Printable Version Email this Page Display Modes Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home General Discussion     Chit Chat LotRO     General Discussion & Support         Interface Help (L)         Interface Requests (L)         Class Discussions (L)         Script & Skin Managers     Interface Downloads Discussion         Released Interfaces (L)         Beta Interfaces (L)         Pre-Beta Interfaces (L)         Dev Tools (L)     Developer Discussions         Tutorials & Other Helpful Information (L)         General Authoring Discussion (L)         Lua Programming Help (L)         XML modification help (L)         Graphics modification help (L)         Preview Server (L)         LotRO Wish List (L) DDO     General Discussion & Support         Interface Help (D)         Interface Requests (D)         Class Discussions (D)     Interface Downloads Discussion         Released Interfaces (D)         Beta Interfaces (D)         Pre-Beta Interfaces (D)         Dev Tools (D)     Developer Discussions         Tutorials & Other Helpful Information (D)         General Authoring Discussion (D)         XML modification help (D)         Graphics modification help (D)         Preview Server (D)         DDO Wish List (D) Site Forums     News     Site help, bugs, suggestions/questions Similar Threads Thread Thread Starter Forum Replies Last Post How does ipairs handle deletes from a table? Niwashi Lua Programming Help (L) 1 05-21-2012 05:29 AM Saving quickslot data to table Marll Lua Programming Help (L) 8 01-07-2011 02:09 PM LocalPlayer (a table value)? WTH goldbishop Lua Programming Help (L) 4 11-30-2010 05:13 PM nested table issues SanDBoX Lua Programming Help (L) 1 11-21-2010 02:12 AM Whats on the table post retail launch? AncientWolf General Authoring Discussion (L) 10 04-27-2007 07:11 AM

All times are GMT -5. The time now is 02:29 PM. Our Network EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui