Jump to content

Sorting a list!?


_DrXenon

Recommended Posts

Hello community,

I have been trying to create a list which is updated every second (based onClientRender event and made of dx) and Includes the names of the online players ingame, this list probably shows random sorting for players, while I want it to sort them by their current money balance. I retrieve the online players list by getElementsByType function.. Anyway, any ideas?

Link to comment
local players = getElementsByType("player")
local sorted = {}

for i=1,#players do
	sorted[i] = {name=getPlayerName(players[i]),money=getPlayerMoney(players[i])}
end
table.sort(sorted,function(a,b) return a.money < b.money end)

-- in draw example:

addEventHandler("onClientRender", root, function()
	local y = y and y + 10 or 0
	for i=1,#sorted do
		dxDrawText("Name: "..sorted[i].name.." Money: "..sorted[i].money, 142, 370 + y, 610, 405, tocolor(34, 220, 60, 255), 1.25, "default", "left", "top", false, false, false, false, false)
	end
end)

Hope this helps :)

Link to comment

Cool. I just noticed a made a minor mistake in the draw although you probably won't use it since you're already drawing the list. It should be:

addEventHandler("onClientRender", root, function()
	local y = 0
	for i=1,#sorted do
		y = i > 1 and y + 10 or y
		dxDrawText("Name: "..sorted[i].name.." Money: "..sorted[i].money, 142, 370 + y, 610, 405, tocolor(34, 220, 60, 255), 1.25, "default", "left", "top", false, false, false, false, false)
	end
end)

 

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...