local function getMaxKey(t)
local maxKey, maxValue = nil, 0
for key, value in pairs(t) do
if value > maxValue then
maxValue = value
maxKey = key
end
end
return maxKey
end
local function getMaxKey(t)
local maxKey, maxValue = nil, 0
for key, value in pairs(t) do
if value > maxValue then
maxValue = value
maxKey = key
end
end
return maxKey
end
local t = {
["Hi"] = 2;
["Bye"] = 15;
["Good"] = 123;
}
local maxKey = getMaxKey(t)
print(maxKey) --Good
Since table.sort doesn’t work with indexes, I would do it like this:
local tbl = {
["Hi"] = 2;
["Bye"] = 15;
["Good"] = 123;
}
local wi = {}
for i,v in pairs(tbl) do
table.insert(wi, {i, v})
end
table.sort(wi, function(a, b)
return a[2] > b[2]
end)
print(wi[1][1] .. "has the highest value of " .. wi[1][2])