ordered_keys = function(t) local result = {} local s = {} for team in pairs(t) do table.insert(result, team) s[team] = score(t, team) end table.sort(result, function(t1, t2) return s[t1] > s[t2] end) return result end