pepsi18 Posted June 26, 2017 Share Posted June 26, 2017 tops = { ["guest1"] = {points = 100, money = "198232"} ["rubi1416"] = {points = 30, money = "2323"} } table.sort(tops, function(a,b) return (tonumber(a.points)or 0) > (tonumber(b.points)or 0) end) How can I sort this table by the number of points? Link to comment
Tails Posted June 26, 2017 Share Posted June 26, 2017 (edited) You can't sort a named key table directly. You could do something like this: function getPoints() local points = {} for k,v in pairs(tops) do table.insert(points, {name = k, points = v.points}) end table.sort(points, function(a,b) return a.points < b.points end) return points end local points = getPoints() outputChatBox(points[1].name.." got the most points! Total: "..points[1].points) Edited June 26, 2017 by Tails Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now