r/lua•
Mortimerlol

Help comparison tables

I have this code: local BangDB = CreateFrame("FRAME"); BangDB:RegisterEvent("ADDON_LOADED"); BangDB:RegisterEvent("PLAYER_ENTERING_WORLD");

local function eventHandler(self, event, ...)
    if event == "ADDON_LOADED" and ... == "Bang_Bang" then
        if not BangCharKills then BangCharKills = {} end
        if not BangCharHateds then BangCharHateds = {} end
 
        self:UnregisterEvent("ADDON_LOADED");
    end
 
-- SORT TABLE A BY NAMES - format{ ["Name"] = true, ["Nametwo"] = true, ... }
local hates = {}
    for k in pairs(BangCharHateds) do hates[#hates + 1] = k end
        table.sort(hates)
    for i = 1, #hates do print(hates[i]) end
 
-- SORT TABLE B BY KILLS, NAMES - format{ ["Name"] = 12, ["Nametwo"] = 5, ... }
function spairs(BangCharKills, order)
    local tokills = {}
    for k in pairs(BangCharKills) do tokills[#tokills + 1] = k end
    if order then
        table.sort(tokills, function(a,b) return order(BangCharKills, a, b) end)
    else
        table.sort(tokills)
    end
    local i = 0
    return function()
        i = i + 1
        --testing with:
        --if tokills[i] == hates[i] then -- doesn't works
        --if BangCharKills[tokills[i]] == hates[i] then -- doesn't works
        if tokills[i] then
            return tokills[i], BangCharKills[tokills[i]]
        end
    end
end
    for k,v in spairs(BangCharKills, function(BangCharKills,a,b) return BangCharKills[b] < BangCharKills[a] end)         do
        print(v, k)
    end
end
BangDB:SetScript("OnEvent", eventHandler);

All works fine but now I need sort TABLE B with (number, name) but if name is in TABLE A...

Thanks for help!

Posted Jul 31, 2017, 2:36 PM UTC

13
Loading comments…