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