How would I be able to sort frames based off of a value inside them?

I am looking to sort some frames based off of a text inside the frame, I tried multiple things but it wont work

This is my function that puts frames for a pet depending on how many pets are in the players inv

local function refreshInventory()	
	for i, v in pairs(script.Parent.InventoryFrame.PetsHolder:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
	
	for i, v in pairs(player.EquippedPets:GetChildren()) do
		local sampleClone = script.Parent.InventoryFrame.SamplePet:Clone()
		sampleClone.Parent = script.Parent.InventoryFrame.PetsHolder
		sampleClone.Name = v.Name
		sampleClone.BackgroundColor3 = Color3.new(0.168627, 0.764706, 0.0784314)
		sampleClone.Power.Text = v.Power.Value
		sampleClone.Visible = true
	end
	
	for i, v in pairs(player.Pets:GetChildren()) do
		local sampleClone = script.Parent.InventoryFrame.SamplePet:Clone()
		sampleClone.Parent = script.Parent.InventoryFrame.PetsHolder
		sampleClone.Name = v.Name
		sampleClone.Power.Text = v.Power.Value
		sampleClone.Visible = true
	end
	
	table.sort(script.Parent.InventoryFrame.PetsHolder:GetChildren(), function(a,b)
		if a:IsA("Frame") and b:IsA("Frame") then
			return tostring(a.Power.Text) > tostring(b.Power.Text)
		end
	end)
end
1 Like

I think there should be tonumber instead of tostring here

Just realized that, let me test it

It still doesn’t work sadly, any other solutions?

I managed to find a solution using UIGridLayout and the LayoutOrder property.

Heres the full script i used:

local Holder = script.Parent.Holder
local Create = script.Parent.Create
local Refresh = script.Parent.Refresh

local RefreshInventory = function()
	local Holder = script.Parent.Holder
	local PowerValues = {}
	local Frames = {}
	
	for i,v in pairs(Holder:GetChildren()) do 
		if (v:IsA("Frame")) and (v.Name ~= "SamplePet") then 
			table.insert(PowerValues, tonumber(v.Power.Text))
			table.insert(Frames, v)
		end
	end
	
	table.sort(PowerValues, function(A, B)
		return A > B
	end)
	
	for i,v in ipairs(Frames) do
		v.LayoutOrder = #Frames - table.find(PowerValues, tonumber(v.Power.Text))
	end
end

--// You probably dont need the code below this.

Create.MouseButton1Down:Connect(function()
	local Info = {}
	
	for i = 1, 10 do 
		table.insert(Info, math.random(1, 100))
	end
	
	for i,v in pairs(script.Parent.Holder:GetChildren()) do 
		if (v:IsA("Frame")) and (v.Name ~= "SamplePet") then 
			v:Destroy()
		end
	end
	
	for i,v in pairs(Info) do 
		local Frame = Holder.SamplePet:Clone()
		Frame.Name = "New"
		Frame.Parent = Holder
		Frame.Visible = true
		Frame.Power.Text = tostring(v)
	end
end)


Refresh.MouseButton1Down:Connect(function()
	RefreshInventory()
end)

RobloxStudioBeta_ORVwKgK319

1 Like

Omg it works! Ty for helping me, I really appreciate it!

1 Like

I figured out a actually shorter way of doing this.

local RefreshInventory = function()
	for i,v in pairs(Holder:GetChildren()) do 
		if (v:IsA("Frame")) and (v.Name ~= "SamplePet") then 
			v.LayoutOrder = tonumber(v.Power.Text)
		end
	end
end
1 Like

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