Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How would I put numbers in order from greatest to least?

Asked by
SuperFryX 130
8 years ago

I am making a Highscore gui. Basically, I have a table that includes all the children of a folder named PlayerNames. All of these children are Int Values with a number as their value. How would I rank these numbers from greatest to lowest? Some of the numbers will also likely be the same.

Here is the code I started

for _,v in pairs(PlayerNames:GetChildren()) do
        --The numbers would be v.Value
        --this is where they are sorted

end

1 answer

Log in to vote
2
Answered by 8 years ago

Here's a solution that might work...

local values = {} -- you could probably come up with a better name for this with context

for _, v in pairs(PlayerNames:GetChildren()) do
    table.insert(values, v.Value)
end

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

Essentially, what you do first is put all of the values into a table. After that, I'm using the table.sort method to sort the table. The second parameter of table.sort is a function that Lua uses to compute the relationship between two values.

0
Thanks! Also, how would I get v.Name to go along with v.Value? I'm a bit inexperienced when it comes to tables. SuperFryX 130 — 8y
Ad

Answer this question