How do I get the smallest value in a dictionary, and then print said value along with its key?

In my golf game, after the last player falls in the final hole, each player’s strokes are tallied up and assigned to a key, which is the player’s name. How do I find the smallest value out of them all, and also get its corresponding key?

2 Likes
local smallestValue, smallestKey = math.huge -- The largest number Lua can represent, just makes the loop check code easier to write

for key, value in pairs(dict) do
    if value < smallestValue then
        smallestKey, smallestValue = key, value
    end
end

if smallestKey then
    -- code
end
8 Likes