Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何在 Corona SDK/Lua 的表中获得最大值?
例如,我有这张表:
local table = { ["item1"] = 10, ["item2"] = 20, ["item3"] = 30, ["item4"] = 40, ["item5"] = 50 }
我必须得到 item5 及其值 50 作为答案。
如果您有非数字键,那么排序将不起作用,您只需要手动遍历表来跟踪最大值及其关联键。
local max_val, key = -math.huge for k, v in pairs(your_table) do if v > max_val then max_val, key = v, k end end print(key, max_val)