how your table looks like?
tkill = {{"aa", 10}, {"bb", 2}, {"cc", 5}}
-- Sort the table from high to low
table.sort(tkill, function (a, b) return a[2] > b[2] end)
for i, v in pairs(tkill) do
print(v[1], v[2])
end
-- This will result:
-- aa, 10
-- cc, 5
-- bb, 2
function getKeyFromVal(name)
if (not name) then
return false
end
for i, v in pairs(tkill) do
if (v[1] == name) then
return i
end
end
return false
end
function addkill(name, num)
if (not name or not num) then
return false
end
local key = getKeyFromVal(name)
tkill[key][2] = tkill[key][2] + num
end
-- we add 6 kill to the "bb"
addkill("bb", 6)
-- sort again
table.sort(tkill, function (a, b) return a[2] > b[2] end)
for i, v in pairs(tkill) do
print(v[1], v[2])
end
--This will result
-- aa, 10
-- bb, 8
-- cc, 5