Jump to content

help table.sort


pepsi18

Recommended Posts

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

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 by Tails
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...