How would I have a LayoutOrder SortOrder be ordered alphabetically ASWELL?

Just to clarify, yes I know this isn’t possible to do with normal sortorder settings. I’m looking for some code within a script that’ll do it.
I don’t wanna have the SortOrder be sorted by name ONLY. It is required for the SortOrder to be LayoutOrder. I just want it to be alphabetical aswell.

This is it with Just LayoutOrder, sorted by difficulty yes, but not sorted alphabetically aswell.


This is how I want it to be. Sorted by both difficulty and alphabetically. (Regular Lamp is first because he’s the star, but I’m sure I can code that by myself.)

I searched it up already and found one post but it didn’t work for me.

Any feedback on the script itself would also be great, I am a learning scripter and want to know if I’m doing anything wrong in my scripts.
Help is greatly appreciated!

local Player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local UserId = Player.UserId

local EasyC = script.Parent.Difficulties.Easy
local MediumC = script.Parent.Difficulties.Medium
local HardC = script.Parent.Difficulties.Hard

local Lampdex = script.Parent.Parent.Parent.LampdexMain
local Lampinfo = script.Parent.Parent.Parent.LampMoreInfoTab

local Owned = script.OwnedLamps
local Total = script.TotalLamps

script.OwnedLamps:GetPropertyChangedSignal("Value"):Connect(function()
	local Ratio = Owned.Value / Total.Value
	Ratio = math.floor(Ratio * 100 + 0.5)
	script.Parent.Parent.LampdexProgress.Text = tostring(Owned.Value) .. "/" .. tostring(Total.Value) .. " | " .. Ratio .. "%"
end)

for _,Lamp in pairs(workspace.Lamps:GetChildren()) do
	Total.Value = Total.Value + 1
	
	local Template = game.ServerStorage.LampTemplate:Clone()
	local BadgeId = Lamp.BadgeId.Value
	
	Template.Name = Lamp.Name
	Template.LampName.Text = Lamp.Name
	Template.LampImageFrame.LampImage.Image = "rbxassetid://" .. Lamp.ImageId.Value
	Template.LampDescription.Value = Lamp.Description.Value
	Template.LampHint.Value = Lamp.Hint.Value
	Template.LampDesigner.Value = Lamp.Designer.Value
	Template.Parent = script.Parent
	
	if Lamp.Difficulty.Value == "Easy" then
		Template.LampName.Text = Lamp.Name
		EasyC:Clone().Parent = Template.LampName
		Template.LayoutOrder = -3
	elseif Lamp.Difficulty.Value == "Medium" then
		Template.LampName.Text = Lamp.Name
		MediumC:Clone().Parent = Template.LampName
		Template.LayoutOrder = -2
	elseif Lamp.Difficulty.Value == "Hard" then
		Template.LampName.Text = Lamp.Name
		HardC:Clone().Parent = Template.LampName
		Template.LayoutOrder = -1
	end
	
	local success, hasBadge = pcall(function()
		return BadgeService:UserHasBadgeAsync(UserId, BadgeId)
	end)
	
	if hasBadge then
		Template.LampImageFrame.BackgroundColor3 = Color3.fromRGB(173, 255, 160)
		Template.LampImageFrame.UIGradient.Color = ColorSequence.new({
			ColorSequenceKeypoint.new(0, Color3.fromRGB(255, 255, 255)),
			ColorSequenceKeypoint.new(0.499, Color3.fromRGB(255, 255, 255)),
			ColorSequenceKeypoint.new(0.5, Color3.fromRGB(131, 204, 174)),
			ColorSequenceKeypoint.new(1, Color3.fromRGB(131, 204, 174))
		})
		Owned.Value = Owned.Value + 1
	end
	
	local Ratio = Owned.Value / Total.Value
	Ratio = math.floor(Ratio * 100 + 0.5)
	script.Parent.Parent.LampdexProgress.Text = tostring(Owned.Value) .. "/" .. tostring(Total.Value) .. " | " .. Ratio .. "%"
end```

Keep the SortOrder as LayoutOrder, you can calculate sort order manually.
To calculate it, it’s pretty simple:

  1. Get the difficulties and names of every element,
  2. Use table.sort to sort the elements, with a comparison function that uses the < operator to compare the difficulty and name,
  3. Apply the resulting order.
type Element = {
	Name: string,
	Difficulty: number,
}

-- Array of elements
local elements = {
	{
		Name = "A",
		Difficulty = 1,
	},
	{
		Name = "B",
		Difficulty = 2,
	},
	{
		Name = "C",
		Difficulty = 1,
	},
	{
		Name = "D",
		Difficulty = 2,
	},
} :: {Element}

-- Sort elements by difficulty and alpha-numerically by name
table.sort(elements, function(a: Element, b: Element)
	return a.Difficulty < b.Difficulty and a.Name < b.Name
end)

print(elements) --> A < C < B < D