How do I sort numbers?

How do I utilize table.sort to sort numerical values in ascending/descending order in this case?

    local values = {}
	for i,child in pairs(players:GetPlayers()) do
		if child.leaderstats ~= nil then
			if child.leaderstats.Elimination then
				table.Insert(values,child)
			end
		end
	end	

Thanks. :pray:

6 Likes

This post could be helpful for you! Please refrain from asking questions that have already been asked: How do you use table.sort?

Note there are a few other threads on table.sort() as well

Please Use The Search Bar

As @mario118118 mentioned, this question has been asked before. Please make use of the search bar next time.

Serveral Questions

It’s hard to tell how your data is organized. Where does a player’s score come from? How do you want it sorted? Where are the numerical values? You do not insert any numerical values.

Sorting Values

Here is code that requires modification but will work with the above information. table.sort() takes a table and a function, the function will give the two values to sort. The functions returns true if the first value should go before the second. All that you have to modify is the values variable. It’s a array of arrays, which are formatted in {player, value}.

local values = { 
	{"Player1", 3},
	{"Player2", 1},
	{"Player3", 2},
}
table.sort(values, 
	function(a,b)--Sorts values
		return a[2]>b[2] --If a is greater than b, then put a above of b. To make lowest values go first, do a[2]<b[2]
	end
)

for i,v in pairs(values) do--Print result
	print(v[1],v[2])
end

image

3 Likes

If Elimination is the value that you’re trying to sort by, then your code might look like this:

local values = {}
for i,player in pairs(players:GetPlayers()) do
	if (player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChidl("Elimination")) then
		table.insert(values, {Player = player, Elimination = player.leaderstats.Elimination.Value})
	end
end

table.sort(values, function(a, b)
	return (a.Elimination < b.Elimination)
end)

That will sort in ascending order. For descending order, change the “<” to a “>” in the sort function. I also used FindFirstChild to check if objects exist. This is necessary when checking for objects in the data model.

2 Likes

It printed nil?

local values = {}

for i,child in pairs (players:GetPlayers()) do
	if child:FindFirstChild("leaderstats") ~= nil then
		
		table.insert(values, {Player = child, Elimination = child.leaderstats.Elimination.Value})
	end
end

if values ~= nil then
	table.sort(values, function(a,b)
		return (a.Elimination < b.Elimination)
	end)
end

for i,v in pairs(values) do--Print result
	print(v[1],v[2])
end

My ultimate goal is to find the player with the most elimination.

If to want to set Player and Elimination, then you can’t do v[1] and v[2], you have to do v.Player and v.Elimination. You just didn’t print the right index, which is why nil was returned. Also, you do not need to check if a table is nil (It never will be, but you could use if #values>=1 to make sure the table has at least 2 indexes to sort. table.sort will still function fine with these cases though.

({} ~= nil) == false

Code adapted to how you store the values:

local values = {}

for i,child in pairs (players:GetPlayers()) do
	if child:FindFirstChild("leaderstats") ~= nil then
		
		table.insert(values, {Player = child, Elimination = child.leaderstats.Elimination.Value})
	end
end


table.sort(values, function(a,b)
	return (a.Elimination < b.Elimination)
end)

for i,v in pairs(values) do--Print result
	print(v.Player,v.Elimination)
end
11 Likes

How do I find the first data in the table?
print(values[1])?

values[1].Player = Player object that got first
values[1].Elimination = Score of that player.

values[2].Player is second, then ect.

1 Like

Thanks for helping! :smiley: I’m not really familiar on tables.

Here’s a little explanation that should help you in the furture:

local a = {1,4,5}

a[1] = 1
a[2] = 4
a[3] = 5

local b = {SpecialIndex = 5, ReallyCoolIndex = 10]

b[‘SpecialIndex’] = 5
b[‘ReallyCoolIndex’] = 10

Arrays store values at specific indexes, such as strings, numbers, or even instances. Doing SpecialIndex = 5 is the same as b.SpecialIndex = 5 and b['SpecialIndex'] = 5]

4 Likes