Problem with sorting an array

Code

for index,contestant in pairs(contestants) do
	local total = 0
	local plrs = 0
	for i,v in pairs(globalData) do
		if not excluded[tostring(v["ID"])] then
			local individualRating = v["Ratings"][index]
			if individualRating then
				total = total+individualRating
				plrs+=1
			end
		end
	end
	local average = total/plrs
	ratings[contestant["Name"]] = {
		["Score"] = average,
		["ID"] = contestant["UserId"]
	}
end
table.sort(ratings, function(a,b) return a.Score > b.Score end)
print(ratings)

Here’s the array it prints:

I want it to sort the table based on the scores. High to low.

1 Like

This is likely the solution you need.

Modify your data returned to include the user’s name with the ID and Score…
image

Test Code


local ratings = {
	{
		["ID"] = "10000",
		["Username"] = "LordDragoniteEz",
		["Score"] = 40
	},
	{
		["ID"] = "10001",
		["Username"] = "NotYoSurf",
		["Score"] = 77
	},
	{
		["ID"] = "10002",
		["Username"] = "ThenewJecan457",
		["Score"] = 53
	}
}

table.sort(ratings, function(a,b)
	print("ATTEMPTING TO SORT",a.Score,b.Score)
	return a.Score > b.Score
end)
print(ratings)
1 Like