How do you make the LayoutOrder properly alphabetical in UIGridLayout

Hello, so basically I am currently working on a game that has many different characters hidden around the map. The issue is that whenever a Frame got cloned or added to the GUI It didn’t sort the name properly to each difficulty. Is there an easier way to make the LayoutOrder properly alphabetical?
RobloxScreenShot20221018_225952197 (2)

1 Like

There is no way to have both Alphabetical order + Layout order using Roblox elements so you’ll have to do this manually via a script.

Steps I would take


  • UIGridLayout

    • Set the sort type to LayoutOrder
  • Obtain all the collectibles

    • For each difficulty make its own table
    • Name them the collectible name
  • Create the templates

    • Naming them only
  • Sorting each difficulty table

    • Using an alphabetical algorithm we sort the table via the name.

    • Function: table.sort(TABLE, function(a,b) return a.Name < b.Name end)

  • Add all the templates in order

    • Set layout order depending on the difficulty
    • Loop through each sorted table to get alphabetical order.

Summary


We are sorting all the tables before adding them to the GUI. If you add an object into a scrollingframe, it will be added to the back unless its layout order is different.

Code:


Keep in mind I have no idea how you get your collectibles so change the script to benefit yours.

function Load()
	local CollectableFolder = game.Players.LocalPlayer.Collectables:GetChildren() -- I'm using this as my example

	local BulkTable = {
		["Difficulty1"] = {};
		["Difficulty2"] = {};
	}

	for _, collectable in pairs(CollectableFolder) do
		if collectable.Difficulty.Value == 1 then -- Checks the difficulty
			local temp = template:Clone() -- Make your template
			temp.Name = collectable.Name
			temp.LayoutOrder = 1
			
			BulkTable.Difficulty1[collectable.Name] = temp -- Places inside correct table
		elseif collectable.Difficulty.Value == 2 then -- Checks the difficulty
			local temp = template:Clone() -- Make your template
			temp.Name = collectable.Name
			temp.LayoutOrder = 2
			
			BulkTable.Difficulty2[collectable.Name] = temp -- Places inside correct table
		end
	end
	
	for _,tbl in pairs(BulkTable) do
		table.sort(tbl, function(a,b) return a<b end) -- Change < to > if you want it sorted the other way.
	end
	
	for _,tbl in pairs(BulkTable) do -- Goes through each difficulty
		for index, object in pairs(tbl) do -- Goes through table alphabetically since we sorted it at line 24
			object.Parent = scrollingFrame -- Places in scrollFrame
		end
	end
	
end


2 Likes

The script didn’t work, It got an error that is “invalid argument #1 to ‘sort’ (table expected, got Instance)”

That means you aren’t feeding it a table but an object, try debugging what the object is.

It still doesn’t work, am I doing It wrong?

Show me the code section of sorting + context about each variable and function.

Hello, sorry for the late response but I might as well tell you how my collectible system works. So basically how It works Is that the script will make “pairs” on every collectible inside a folder in Workspace and then make duplicates of template frames inside the GUI. But anyways here’s the code (I don’t have a section for sorting)

local player = script.Parent.Parent.Parent.Parent.Parent
local UserId = player.UserId

local total = script.Total

local BulkTable = {
	["Difficulty1"] = {};
	["Difficulty2"] = {};
	["Difficulty3"] = {};
	["Difficulty4"] = {};
	["Difficulty5"] = {};
	["Difficulty6"] = {};
	["Difficulty7"] = {};
}

for _,bloxikin in pairs(workspace.Bloxikins:GetChildren()) do
	total.Value = total.Value +1
	local template = game.ServerStorage.BloxikinTemplate:Clone()
	local DecalId = bloxikin.Configuration.DecalId.Value
	local BloxName = bloxikin.Configuration.BloxName.Value
	local Description = bloxikin.Configuration.Description.Value
	local Hint = bloxikin.Configuration.Hint.Value
	
	template.Name = BloxName
	template.BloxikinName.Text = BloxName
	template.Parent = script.Parent.Frame
	template.ImageLabel.Image = DecalId
	template.Description.Value = Description
	template.Hint.Value = Hint
	
	if bloxikin.Configuration.Difficulty.Value == "Easy" then
		template.Difficulty.Text = "Easy"
		template.LayoutOrder = -7
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(29, 223, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(17, 131, 0)
		
		BulkTable.Difficulty1[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Medium" then
		template.Difficulty.Text = "Medium"
		template.LayoutOrder = -6
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 162, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(176, 111, 0)
		
		BulkTable.Difficulty2[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Hard" then
		template.Difficulty.Text = "Hard"
		template.LayoutOrder = -5
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(161, 0, 2)
		
		BulkTable.Difficulty3[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Insane" then
		template.Difficulty.Text = "Insane"
		template.LayoutOrder = -4
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(80, 83, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(50, 56, 166)
		
		BulkTable.Difficulty4[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Extreme" then
		template.Difficulty.Text = "Extreme"
		template.LayoutOrder = -3
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(0, 187, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 125, 171)
		
		BulkTable.Difficulty5[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Terrifying" then
		template.Difficulty.Text = "Terrifying"
		template.LayoutOrder = -2
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(1, 47, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 30, 163)
		
		BulkTable.Difficulty6[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Nightmare" then
		template.Difficulty.Text = "Nightmare"
		template.LayoutOrder = -1
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(71, 4, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(59, 3, 3)
		
		BulkTable.Difficulty7[template.Name] = template
	end
	
	for _,tbl in pairs(BulkTable) do
		table.sort(tbl, function(a,b) return a<b end) -- Change < to > if you want it sorted the other way.
	end
	
	--Collabs (TEST)
	if bloxikin.Configuration.Collab.Value == "None" then
		template.CollabConfiguration.CollabName.Value = "None"
	elseif bloxikin.Configuration.Collab.Value == "Find The Chomiks" then
		template.CollabConfiguration.CollabName.Value = "Find The Chomiks"
		template.CollabConfiguration.CollabValue.Value = "7031179594"
	end

	
	if bloxikin.Configuration.Series.Value == "None" then
		template.Series.Visible = false
		template.Series.SearchValue.Value = "None"
	elseif bloxikin.Configuration.Series.Value == "Secret" then
		template.Series.Visible = true
		template.Series.SearchValue.Value = "Secret"
		template.Series.Image = "rbxassetid://11234662572"
	end
	
	-- REALMS LIST:
	-- 1. Obby
	-- 2. Backrooms
	
	if bloxikin.Configuration.Realm.Value == 0 then
		template.Realm.Visible = false
		template.Realm.SearchValue.Value = "Normal"
	elseif bloxikin.Configuration.Realm.Value == 1 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Obby"
		template.Realm.Image = "rbxassetid://11240718407"
	elseif bloxikin.Configuration.Realm.Value == 2 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Backrooms"
		template.Realm.Image = "rbxassetid://11240718688"
	end
end```

I see your problem, you’ve already added it to the frame before the sorting system:

image

I suggest removing that and using a loop to add all the stuff otherwise it will still not be sorted in both layout and alphabetical order:

local player = script.Parent.Parent.Parent.Parent.Parent
local UserId = player.UserId

local total = script.Total

local BulkTable = {
	["Difficulty1"] = {};
	["Difficulty2"] = {};
	["Difficulty3"] = {};
	["Difficulty4"] = {};
	["Difficulty5"] = {};
	["Difficulty6"] = {};
	["Difficulty7"] = {};
}

for _,bloxikin in pairs(workspace.Bloxikins:GetChildren()) do
	total.Value = total.Value +1
	local template = game.ServerStorage.BloxikinTemplate:Clone()
	local DecalId = bloxikin.Configuration.DecalId.Value
	local BloxName = bloxikin.Configuration.BloxName.Value
	local Description = bloxikin.Configuration.Description.Value
	local Hint = bloxikin.Configuration.Hint.Value

	template.Name = BloxName
	template.BloxikinName.Text = BloxName
	template.ImageLabel.Image = DecalId
	template.Description.Value = Description
	template.Hint.Value = Hint

	if bloxikin.Configuration.Difficulty.Value == "Easy" then
		template.Difficulty.Text = "Easy"
		template.LayoutOrder = -7
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(29, 223, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(17, 131, 0)

		BulkTable.Difficulty1[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Medium" then
		template.Difficulty.Text = "Medium"
		template.LayoutOrder = -6
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 162, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(176, 111, 0)

		BulkTable.Difficulty2[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Hard" then
		template.Difficulty.Text = "Hard"
		template.LayoutOrder = -5
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(161, 0, 2)

		BulkTable.Difficulty3[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Insane" then
		template.Difficulty.Text = "Insane"
		template.LayoutOrder = -4
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(80, 83, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(50, 56, 166)

		BulkTable.Difficulty4[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Extreme" then
		template.Difficulty.Text = "Extreme"
		template.LayoutOrder = -3
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(0, 187, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 125, 171)

		BulkTable.Difficulty5[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Terrifying" then
		template.Difficulty.Text = "Terrifying"
		template.LayoutOrder = -2
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(1, 47, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 30, 163)

		BulkTable.Difficulty6[template.Name] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Nightmare" then
		template.Difficulty.Text = "Nightmare"
		template.LayoutOrder = -1
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(71, 4, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(59, 3, 3)

		BulkTable.Difficulty7[template.Name] = template
	end

	for _,tbl in pairs(BulkTable) do
		table.sort(tbl, function(a,b) return a<b end) -- Change < to > if you want it sorted the other way.
	end

	--Collabs (TEST)
	if bloxikin.Configuration.Collab.Value == "None" then
		template.CollabConfiguration.CollabName.Value = "None"
	elseif bloxikin.Configuration.Collab.Value == "Find The Chomiks" then
		template.CollabConfiguration.CollabName.Value = "Find The Chomiks"
		template.CollabConfiguration.CollabValue.Value = "7031179594"
	end


	if bloxikin.Configuration.Series.Value == "None" then
		template.Series.Visible = false
		template.Series.SearchValue.Value = "None"
	elseif bloxikin.Configuration.Series.Value == "Secret" then
		template.Series.Visible = true
		template.Series.SearchValue.Value = "Secret"
		template.Series.Image = "rbxassetid://11234662572"
	end

	-- REALMS LIST:
	-- 1. Obby
	-- 2. Backrooms

	if bloxikin.Configuration.Realm.Value == 0 then
		template.Realm.Visible = false
		template.Realm.SearchValue.Value = "Normal"
	elseif bloxikin.Configuration.Realm.Value == 1 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Obby"
		template.Realm.Image = "rbxassetid://11240718407"
	elseif bloxikin.Configuration.Realm.Value == 2 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Backrooms"
		template.Realm.Image = "rbxassetid://11240718688"
	end
	
	--["This adds all the templates in order"]
	
	for _,tbl in pairs(BulkTable) do 
		for index, object in pairs(tbl) do
			object.Parent = script.Parent.Frame
		end
	end
end

It still doesn’t work. Even if you put it on a loop It breaks the script


local player = script.Parent.Parent.Parent.Parent.Parent
local UserId = player.UserId

local total = script.Total

local BulkTable = {
	["Difficulty1"] = {};
	["Difficulty2"] = {};
	["Difficulty3"] = {};
	["Difficulty4"] = {};
	["Difficulty5"] = {};
	["Difficulty6"] = {};
	["Difficulty7"] = {};
}

for _,bloxikin in pairs(workspace.Bloxikins:GetChildren()) do
	total.Value = total.Value +1
	local template = game.ServerStorage.BloxikinTemplate:Clone()
	local DecalId = bloxikin.Configuration.DecalId.Value
	local BloxName = bloxikin.Configuration.BloxName.Value
	local Description = bloxikin.Configuration.Description.Value
	local Hint = bloxikin.Configuration.Hint.Value

	template.Name = BloxName
	template.BloxikinName.Text = BloxName
	template.ImageLabel.Image = DecalId
	template.Description.Value = Description
	template.Hint.Value = Hint

	if bloxikin.Configuration.Difficulty.Value == "Easy" then
		template.Difficulty.Text = "Easy"
		template.LayoutOrder = -7
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(29, 223, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(17, 131, 0)

		BulkTable.Difficulty1[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Medium" then
		template.Difficulty.Text = "Medium"
		template.LayoutOrder = -6
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 162, 0)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(176, 111, 0)

		BulkTable.Difficulty2[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Hard" then
		template.Difficulty.Text = "Hard"
		template.LayoutOrder = -5
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(161, 0, 2)

		BulkTable.Difficulty3[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Insane" then
		template.Difficulty.Text = "Insane"
		template.LayoutOrder = -4
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(80, 83, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(50, 56, 166)

		BulkTable.Difficulty4[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Extreme" then
		template.Difficulty.Text = "Extreme"
		template.LayoutOrder = -3
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(0, 187, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 125, 171)

		BulkTable.Difficulty5[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Terrifying" then
		template.Difficulty.Text = "Terrifying"
		template.LayoutOrder = -2
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(1, 47, 255)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(0, 30, 163)

		BulkTable.Difficulty6[BloxName] = template
	elseif bloxikin.Configuration.Difficulty.Value == "Nightmare" then
		template.Difficulty.Text = "Nightmare"
		template.LayoutOrder = -1
		template.Difficulty.BackgroundColor3 = Color3.fromRGB(71, 4, 4)
		template.Difficulty.DifficultyValue.Value = Color3.fromRGB(59, 3, 3)

		BulkTable.Difficulty7[BloxName] = template
	end
	
	--Collabs (TEST)
	if bloxikin.Configuration.Collab.Value == "None" then
		template.CollabConfiguration.CollabName.Value = "None"
	elseif bloxikin.Configuration.Collab.Value == "Find The Chomiks" then
		template.CollabConfiguration.CollabName.Value = "Find The Chomiks"
		template.CollabConfiguration.CollabValue.Value = "7031179594"
	end


	if bloxikin.Configuration.Series.Value == "None" then
		template.Series.Visible = false
		template.Series.SearchValue.Value = "None"
	elseif bloxikin.Configuration.Series.Value == "Secret" then
		template.Series.Visible = true
		template.Series.SearchValue.Value = "Secret"
		template.Series.Image = "rbxassetid://11234662572"
	end

	-- REALMS LIST:
	-- 1. Obby
	-- 2. Backrooms

	if bloxikin.Configuration.Realm.Value == 0 then
		template.Realm.Visible = false
		template.Realm.SearchValue.Value = "Normal"
	elseif bloxikin.Configuration.Realm.Value == 1 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Obby"
		template.Realm.Image = "rbxassetid://11240718407"
	elseif bloxikin.Configuration.Realm.Value == 2 then
		template.Realm.Visible = true
		template.Realm.SearchValue.Value = "Backrooms"
		template.Realm.Image = "rbxassetid://11240718688"
	end
	
	for _,tbl in pairs(BulkTable) do
		table.sort(tbl, function(a,b) return a<b end)
	end

	for _,tbl in pairs(BulkTable) do 
		for index, object in pairs(tbl) do
			object.Parent = script.Parent.Frame
		end
	end
end