How can I sort objects in the workspace from least to greatest?

Hi forum. In my game, I have a folder with 9 parts inside, each named 1 - 9 respectively. In another folder, there are 9 IntValues inside. Each IntValue’s value is given a random number.

I want to know how you would sort the IntValues from least to greatest, and then set the parent of each IntValue to the part inside the other folder that match the IntValue’s ranking.

For example, the IntValue with the lowest value would have it’s parent set as the part in the folder named ‘1’, and the IntValue with the highest value would have it’s parent set to the part in the folder named ‘9’.

I have tried searching these forums if anyone had a similar question with a solution, but I am stumped, and do not have enough knowledge to know how to set this up with the basic coding skills I have.

Any help would be greatly appreciated.

Something like this??

I have the int values in a folder in replicated storage
and I have a folder, with 5 folders inside called “Value” and then a number 1 through 5(I did 5 for convenience sake). And they all have a part inside of the folders.

local IntValues = game.ReplicatedStorage.Values:GetChildren()

local Parts = workspace.Parts:GetChildren()

local numbers = {1,2,3,4,5,6,7,8,9,0}

for i,v1 in Parts do
	if v1:IsA("Folder") then
		local foldername = v1.Name
		local num
		
		for i,number in pairs(numbers) do
			local findnumber = string.find(foldername, tostring(number))
			
			if findnumber then
				num = number
				break
			end
		end
		
		if num then
			for i,v in pairs(IntValues) do
				if v.Value == num then
					v.Parent = v1
					break
				end
			end
			
		end
	end
end

I didn’t really understand the problem, so let me know if there’s something that’s needs changing.

Hi! Sorry if my explanation was not clear. Let me try to explain it better: Lets say the values in ReplicatedStorage folder are ‘1, 10, 2, 5, and 8’. I want the IntValues to be sorted from least to greatest across each folder.

So the IntValue with the value ‘1’ would have it’s parent set to the folder ‘Value1’,

the IntValue with the value ‘2’ would have it’s parent set to the folder ‘Value2’,

the IntValue with the value ‘5’ would have it’s parent set to the folder ‘Value3’,

the IntValue with the value ‘8’ would have it’s parent set to the folder ‘Value4’,

and the IntValue with the value ‘10’ would have it’s parent set to the folder ‘Value5’.

Hope this helps.

Like this?

local IntValues = game.ReplicatedStorage.Values:GetChildren()
local Parts = workspace.Parts:GetChildren()

table.sort(IntValues, function(a, b) 
	return a.Value < b.Value 
end)

table.sort(Parts, function(a, b)
	return a.Name < b.Name
end)

local sortedIntValues = {}
local sortedParts = {}

for _, part in ipairs(Parts) do
	if part:IsA("Folder") then
		table.insert(sortedParts, part)
	end
end

for _, value in ipairs(IntValues) do
	table.insert(sortedIntValues, value)
end

for i, intValue in ipairs(sortedIntValues) do
	if i <= #sortedParts then
		intValue.Parent = sortedParts[i].Part
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.