[SOLVED] How can I sort scrolling frames?

I want to sort my scrolling frame by the rarity of the item, there is a StringValue that has the rarity in it inside of each item GUI. For some reason, the code that I made has no errors, and it doesn’t work. I am not sure why, what the code basically does is it moves the GUI into 4 different invisible frames to store them, and then it moves the Easy rarity, then Normal, then Hard, then Insane after the previous rarity is done. For some reason, the game just decides to randomly organize them. This is really frustrating and I need help either fixing the code or coming up with other solutions.
-Peter!
This is the code that I used:

script.Parent.Parent.Parent.Changed:Connect(function()
	if script.Parent.Parent.Parent.Visible == true then
		
		for i,v in pairs(script.Parent.Parent.Parent.List.ScrollingFrame:GetChildren()) do
			if v:FindFirstChild("BadgeId") then
				if v.ImageButton.Rarity.Value == "Easy" then
					v.Parent = script.Parent.Parent.Parent.List.EasyFrame
				end
				if v.ImageButton.Rarity.Value == "Normal" then
					v.Parent = script.Parent.Parent.Parent.List.NormalFrame
				end
				if v.ImageButton.Rarity.Value == "Hard" then
					v.Parent = script.Parent.Parent.Parent.List.HardFrame
				end
				if v.ImageButton.Rarity.Value == "Insane" then
					v.Parent = script.Parent.Parent.Parent.List.InsaneFrame
				end
			end	
		end	
		
		for i,v in pairs(script.Parent.Parent.Parent.List.EasyFrame:GetChildren()) do
			v.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
		end
		
		if #script.Parent.Parent.Parent.List.EasyFrame:GetChildren() == 0 then
			for i,v in pairs(script.Parent.Parent.Parent.List.NormalFrame:GetChildren()) do
				v.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
			end

			
			if #script.Parent.Parent.Parent.List.NormalFrame:GetChildren() == 0 then
				for i,v in pairs(script.Parent.Parent.Parent.List.HardFrame:GetChildren()) do
					v.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
				end

				if #script.Parent.Parent.Parent.List.HardFrame:GetChildren() == 0 then

					for i,v in pairs(script.Parent.Parent.Parent.List.InsaneFrame:GetChildren()) do
						v.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
					end
				end
			end
		end
	end
end)
1 Like

You could use the LayoutOrder property of the GuiObject(s) that is in the ScrollingFrame and set the SortOrder property of UIGridLayout/UIListLayout to LayoutOrder:

1 Like

It was always set to layout order, that is why I am confused about why it doesn’t work.

You can use table.sort and return a and b, so that a>b or b>a [you can name your frames in numbers, to make it simple]

Or use UiGridLayout

Can you share a screenshot of your ScreenGui instance hierarchy? The UILayout suggestion should and does work. There’s a good chance you’ve made a mistake somewhere.

Is anyone gonna help? it’s been 4 days and I need to release soon

Ok, I came up witha possible solution. Go through all the easy rarity things, and set their LayoutOrder to their index. Then, for the medium, do their index * 100. Hard * 250, and insane * 500. (index is the i in i, v in pairs()

here is an example bit of code:

script.Parent.Parent.Parent.Changed:Connect(function()
	if script.Parent.Parent.Parent.Visible == true then
		local Easy = {}
		local Normal = {}
		local Hard = {}
		local Insane = {}
		for i,v in pairs(script.Parent.Parent.Parent.List.ScrollingFrame:GetChildren()) do
			if v:FindFirstChild("BadgeId") then
				if v.ImageButton.Rarity.Value == "Easy" then
					table.insert(Easy,v)
				end
				if v.ImageButton.Rarity.Value == "Normal" then
					table.insert(Normal,v)
				end
				if v.ImageButton.Rarity.Value == "Hard" then
					table.insert(Hard,v)
				end
				if v.ImageButton.Rarity.Value == "Insane" then
					table.insert(Insane,v)
				end
			end	
		end	

		for index, value in ipairs(Easy) do-- for i, v in ipairs() do
			value.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
			value.LayoutOrder = index
		end
		for index, value in ipairs(Normal) do-- for i, v in ipairs() do
			value.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
			value.LayoutOrder = index + 1000
		end
		for index, value in ipairs(Hard) do-- for i, v in ipairs() do
			value.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
			value.LayoutOrder = index + 2000
		end
		for index, value in ipairs(Insane) do-- for i, v in ipairs() do
			value.Parent = script.Parent.Parent.Parent.List.ScrollingFrame
			value.LayoutOrder = index + 3000
		end
	end
end)

Thank you so much! This works perfectly, this is awesome!