Sorting a GetChildren table

image
Hi how can i sort this Ores:GetChildren() based on OreValue?

For example lets say the Amethyst folder has an OreValue of 10 and Azurite has an OreValue of 20
How can i make it so the Amethyst will be before the Azuire in the table?

GetChildren returns a table automatically, so…

for i,v in pairs(myTable)

Yes but that was an example. It returns a table but thats not what i mean. My question is how i can sort that table based on the OreValue.Value, from lowest OreValue.Value to highest.

local dictionary = {}

for _, value in pairs(Ores:GetChildren()) do
dictionary[value.Name] = value.Value;
end

You can try storing them inside the table as a dictionary format.

keys = the names of the values
values = the value of the ValueObject

Okay, but how can i sort that dictionary from lowest to highest?

Just try that for now, and let me know how it goes.

You can also refer to:

I would use a for loop and table.sort(). For example,
local valueTable = {}
local oresTable = {}
for index, value in ipairs(ores:GetDescendants()) do
if value:IsA(“NumberValue”) then
table.insert(valueTable,value.Value) --Adds all the ore values to the table
table.insert(oresTable, value) – Adds all the ore value objects themselves
end
end
local valueTable2 = valueTable --Make a new table to store the valueTable from before sort
–Then we can sort the valueTable, which returns the values sorted
table.sort(valueTable)
–Then we can go through the old values and map them to the new ones
local newOresTable = {}
for index, value in ipairs(valueTable2) do
local newIndex = table.find(valueTable, value) --Gives the new index value
newOresTable[newIndex] = oresTable[index] --Assigns the old table value at the new index of that value
end


I’m sure there’s a better way to do this somehow but this is what I came up with. LMK if it works.

Yes, but he hasn’t defined the Children for GetChildren() yet.

local ores = pathway to ores:GetChildren()
for i = 1, #ores do
    print(i, ores[i].Name)
end

In this method, I’m assuming they are defined as OP describes them,

It’s irrelevant how those values are defined, what matters is the ability to sort them.

I asked a friend

table.sort(ores, function(a,b)
return a.OreValue.Value < b…

@Monkepath 's solution is a good start, but it does not take advantage of table.sort's optional Sort Ranking Function argument. With it, this task is fairly trivial:

Sort Ranking Function

A sort ranking function takes two values as input parameters. The function should return true if the first element should come before the second, and false if the second element should come before the first.

For example, a standard numerical sort ranking function would look something like this:

function LessThanRank(a, b)
    return a < b -- A should come before B if A is less than B.
end

For our situation, we take a table of folders that represent each ore, and we want to sore each one based on their value. So, we can adjust this function to take into account we want to compare values of children of the parameters:

function LessThanRankOre(a, b)
    return a.OreValue.Value < b.OreValue.Value -- A should come before B if A's orevalue is less than B's orevalue.
end

Then, we can just use table.sort like so:

table.sort(tableFromGetChildren, LessThanRankOre)

Hope this helps!

EDIT: Linked documentation

7 Likes