Sorting GUI elements going from the highest of one value to the lowest of the same

I’m using this script to sort the inventory and create the GUI buttons, basing on pet power to sort the “most powerful” pets at the top and go to the “weakest”.

local l__LocalPlayer__1 = game.Players.LocalPlayer;
local l__Pets__2 = l__LocalPlayer__1:WaitForChild("Pets");
local l__ReplicatedStorage__1 = game.ReplicatedStorage;
function getLevel(p1)
	local v3 = 0;
	local l__Value__4 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
	local v5 = 0 - 1;
	while true do
		if 100 * v5 + v3 <= p1 then
			if v5 ~= l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value then

			else
				return v5;
			end;
			if p1 < 100 * v5 + v3 + (100 + 25 * v5) then
				return v5;
			end;
		end;
		v3 = v3 + v5 * 25;
		if 0 <= 1 then
			if v5 < l__Value__4 then

			else
				break;
			end;
		elseif l__Value__4 < v5 then

		else
			break;
		end;
		v5 = v5 + 1;	
	end;
end;
function GetFolderFromPetID(p2)
	local v6, v7, v8 = pairs(l__LocalPlayer__1.Pets:GetChildren());
	while true do
		local v9, v10 = v6(v7, v8);
		if v9 then

		else
			break;
		end;
		v8 = v9;
		if v10.PetID.Value == p2 then
			return v10;
		end;	
	end;
	return nil;
end;
function getLayoutOrder(p3)
	local v11 = GetFolderFromPetID(p3);
	local l__Value__12 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(v11.Name).Settings.Multiplier1.Value;
	local l__Value__13 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
	local v14 = getLevel(v11.TotalXP.Value);
	local v15 = {};
	local v16 = 0;
	local v17, v18, v19 = pairs(l__ReplicatedStorage__1.Pets.Models:GetChildren());
	while true do
		local v20, v21 = v17(v18, v19);
		if v20 then

		else
			break;
		end;
		v19 = v20;
		v15[#v15 + 1] = v21;	
	end;
	table.sort(v15, function(p4, p5)
		return p4:GetFullName() < p5:GetFullName();
	end);
	local v22, v23, v24 = pairs(v15);
	while true do
		local v25 = nil;
		local v26 = nil;
		v26, v25 = v22(v23, v24);
		if v26 then

		else
			break;
		end;
		v24 = v26;
		if v11.Equipped.Value == true then
			if v11.Name == v25.Name then
				return l__Value__13 * #v15 * l__Value__12 + v16 * l__Value__13 + (l__Value__13 - v14);
			end;
		elseif v11.Name == v25.Name then
			return #v15 * #l__ReplicatedStorage__1.Pets.Rarities:GetChildren() * l__Value__13 + l__Value__13 * #v15 * l__Value__12 + v16 * l__Value__13 + (l__Value__13 - v14);
		end;
		v16 = v16 + 1;	
	end;
end;
local l__UIelements__2 = l__LocalPlayer__1.PlayerGui:WaitForChild("UIelements");
local l__Inventory__3 = script.Parent.MainFrame.Inventory;
function newUI(p6)
	local v27 = l__UIelements__2.PetTemplate:Clone();
	local v28 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(p6.Name);
	local v29 = v28:FindFirstChild(p6:WaitForChild("Type").Value):Clone();
	local l__Value__30 = l__ReplicatedStorage__1.Pets.Rarities:FindFirstChild(v28.Settings.Rarity.Value).Color.Value;
	local v31 = Instance.new("Camera", v27.PetView);
	local l__Position__32 = v29.PrimaryPart.Position;
	v27.PetID.Value = p6:WaitForChild("PetID").Value;
	v27.Name = p6.Name;
	v27.Gradient.UIGradient.Color = ColorSequence.new(l__Value__30, (Color3.new(l__Value__30.R - 0.5882352941176471, l__Value__30.G - 0.5882352941176471, l__Value__30.B - 0.5882352941176471)));
	v27.PetView.CurrentCamera = v31;
	v29.Parent = v27.PetView;
	v31.CFrame = CFrame.new(Vector3.new(l__Position__32.X + 2.25, l__Position__32.Y, l__Position__32.Z + 1), l__Position__32);
	v27.Parent = l__Inventory__3;
	l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
	if p6.Equipped.Value == true then
		v27.EquipMarker.Visible = true;
	else
		v27.EquipMarker.Visible = false;
	end;
	p6:WaitForChild("TotalXP"):GetPropertyChangedSignal("Value"):Connect(function()
		v27.LevelLabel.Text = "Lvl. " .. getLevel(p6:WaitForChild("TotalXP").Value);
	end);
	local v33, v34, v35 = pairs(l__Inventory__3:GetChildren());
	while true do
		local v36, v37 = v33(v34, v35);
		if v36 then

		else
			break;
		end;
		v35 = v36;
		if not v37:IsA("UIGridLayout") then
			v37.LayoutOrder = getLayoutOrder(v37.PetID.Value);
		end;	
	end;
end;
for v38, v39 in pairs(l__Pets__2:GetChildren()) do
	newUI(v39);
end;
local l__TextLabel__4 = script.Parent.MainFrame.StorageDisplay.TextLabel;
local l__Data__5 = l__LocalPlayer__1:WaitForChild("Data");
local l__TextLabel__6 = script.Parent.MainFrame.EquippedDisplay.TextLabel;
l__Pets__2.ChildAdded:Connect(function(p7)
	local v40 = 0;
	for v41, v42 in pairs(l__Pets__2:GetChildren()) do
		if v42:WaitForChild("Equipped").Value == true then
			v40 = v40 + 1;
		end;
	end;
	l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
	l__TextLabel__6.Text = v40 .. "/" .. l__Data__5.MaxEquip.Value;
	newUI(p7);
end);
l__Pets__2.ChildRemoved:Connect(function(p8)
	for v43, v44 in pairs(l__Inventory__3:GetChildren()) do
		if not v44:IsA("UIGridLayout") and v44.PetID.Value == p8.PetID.Value then
			v44:Destroy();
		end;
	end;
	local v45 = 0;
	for v46, v47 in pairs(l__Pets__2:GetChildren()) do
		if v47.Equipped.Value == true then
			v45 = v45 + 1;
		end;
	end;
	l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
	l__TextLabel__6.Text = v45 .. "/" .. l__Data__5.MaxEquip.Value;
end);
l__Inventory__3.ChildAdded:Connect(function()
	l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
l__Inventory__3.ChildRemoved:Connect(function()
	l__Inventory__3.CanvasSize = UDim2.new(0, 0, 0, l__Inventory__3.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
local v48 = 0;
for v49, v50 in pairs(l__Pets__2:GetChildren()) do
	if v50.Equipped.Value == true then
		v48 = v48 + 1;
	end;
end;
l__Data__5.MaxStorage:GetPropertyChangedSignal("Value"):Connect(function()
	l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
end);
l__Data__5.MaxEquip:GetPropertyChangedSignal("Value"):Connect(function()
	l__TextLabel__6.Text = v48 .. "/" .. l__Data__5.MaxEquip.Value;
end);
l__TextLabel__4.Text = #l__Pets__2:GetChildren() .. "/" .. l__Data__5.MaxStorage.Value;
l__TextLabel__6.Text = v48 .. "/" .. l__Data__5.MaxEquip.Value;

It gets the values to sort and make here

function getLayoutOrder(p3)
	local v11 = GetFolderFromPetID(p3);
	local l__Value__12 = l__ReplicatedStorage__1.Pets.Models:FindFirstChild(v11.Name).Settings.Multiplier1.Value;
	local l__Value__13 = l__ReplicatedStorage__1.Pets.Settings.MaxPetLevel.Value;
	local v14 = getLevel(v11.TotalXP.Value);
	local v15 = {};
	local v16 = 0;
	local v17, v18, v19 = pairs(l__ReplicatedStorage__1.Pets.Models:GetChildren());

What it currently does is use the pet power but it sorts them the complete opposite way, weakest to strongest and puts the equipped pets at the bottom which it shouldn’t do.
How would I be able to sort of flip the inventory sorting so its from strongest to weakest and the equipped pets stay at the top?

You can specify a custom sorting lambda function for use with the table.sort function, which should do this easily for you.

I mean no offense, but your code is really hard to understand given the variable names you’ve chosen (it’s hard to know what anything means)

But here’s how to use a custom sorting lambda (lambda = anonymous function, a function bound to no identifier)

local pets = { --//random example
{petPower = 30};
{petPower = 14};
{petPower = 92};
}

table.sort(pets, function(a, b)
--//'a' and 'b' are elements in the table that are being sorted
return a.petPower > b.petPower --//Make it so stronger pets are at the start
end)

I have done my best to make the script a little more readable, I wrote this a long time ago so I don’t quite know what most of the highly dependent ones mean, but here is a considerably easier to understand version of it:

local localplayer = game.Players.LocalPlayer;
local pets = localplayer:WaitForChild("Pets");
local replicatedStorage = game.ReplicatedStorage;
function getLevel(p1)
	local v3 = 0;
	local MaxPetLevel = replicatedStorage.Pets.Settings.MaxPetLevel.Value;
	local v5 = 0 - 1;
	while true do
		if 100 * v5 + v3 <= p1 then
			if v5 ~= replicatedStorage.Pets.Settings.MaxPetLevel.Value then

			else
				return v5;
			end;
			if p1 < 100 * v5 + v3 + (100 + 25 * v5) then
				return v5;
			end;
		end;
		v3 = v3 + v5 * 25;
		if 0 <= 1 then
			if v5 < MaxPetLevel then

			else
				break;
			end;
		elseif MaxPetLevel < v5 then

		else
			break;
		end;
		v5 = v5 + 1;	
	end;
end;
function GetFolderFromPetID(p2)
	local v6, v7, v8 = pairs(localplayer.Pets:GetChildren());
	while true do
		local v9, v10 = v6(v7, v8);
		if v9 then

		else
			break;
		end;
		v8 = v9;
		if v10.PetID.Value == p2 then
			return v10;
		end;	
	end;
	return nil;
end;
function getLayoutOrder(p3)
	local PetToSort = GetFolderFromPetID(p3);
	local SortFactor1 = replicatedStorage.Pets.Models:FindFirstChild(PetToSort.Name).Settings.Multiplier1.Value;
	local SortFactor2 = replicatedStorage.Pets.Settings.MaxPetLevel.Value;
	local PetExp = getLevel(PetToSort.TotalXP.Value);
	local PetTable = {};
	local v16 = 0;
	local v17, v18, v19 = pairs(replicatedStorage.Pets.Models:GetChildren());
	while true do
		local v20, v21 = v17(v18, v19);
		if v20 then

		else
			break;
		end;
		v19 = v20;
		PetTable[#PetTable + 1] = v21;	
	end;
	local PetTable = { 
		{petPower = SortFactor1};
		{petPower = SortFactor1};
		{petPower = SortFactor1};
	}

	table.sort(PetTable, function(a, b)
		return a.petPower > b.petPower 
	end)
	local v22, v23, v24 = pairs(PetTable);
	while true do
		local PetInSorting = nil;
		local v26 = nil;
		v26, PetInSorting = v22(v23, v24);
		if v26 then

		else
			break;
		end;
		v24 = v26;
		if PetToSort.Equipped.Value == true then
			if PetToSort.Name == PetInSorting.Name then
				return SortFactor2 * #PetTable * SortFactor1 + v16 * SortFactor2 + (SortFactor2 - PetExp);
			end;
		elseif PetToSort.Name == PetInSorting.Name then
			return #PetTable * #replicatedStorage.Pets.Rarities:GetChildren() * SortFactor2 + SortFactor2 * #PetTable * SortFactor1 + v16 * SortFactor2 + (SortFactor2 - PetExp);
		end;
		v16 = v16 + 1;	
	end;
end;
local UIElements = localplayer.PlayerGui:WaitForChild("UIelements");
local InventoryFrame = script.Parent.MainFrame.Inventory;
function newUI(p6)
	local PetTemplate = UIElements.PetTemplate:Clone();
	local PetModel = replicatedStorage.Pets.Models:FindFirstChild(p6.Name);
	local PetRarity = PetModel:FindFirstChild(p6:WaitForChild("Type").Value):Clone();
	local PetRarityColor = replicatedStorage.Pets.Rarities:FindFirstChild(PetModel.Settings.Rarity.Value).Color.Value;
	local PetCamera = Instance.new("Camera", PetTemplate.PetView);
	local PetPosition = PetRarity.PrimaryPart.Position;
	PetTemplate.PetID.Value = p6:WaitForChild("PetID").Value;
	PetTemplate.Name = p6.Name;
	PetTemplate.Gradient.UIGradient.Color = ColorSequence.new(PetRarityColor, (Color3.new(PetRarityColor.R - 0.5882352941176471, PetRarityColor.G - 0.5882352941176471, PetRarityColor.B - 0.5882352941176471)));
	PetTemplate.PetView.CurrentCamera = PetCamera;
	PetRarity.Parent = PetTemplate.PetView;
	PetCamera.CFrame = CFrame.new(Vector3.new(PetPosition.X + 2.25, PetPosition.Y, PetPosition.Z + 1), PetPosition);
	PetTemplate.Parent = InventoryFrame;
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200);
	if p6.Equipped.Value == true then
		PetTemplate.EquipMarker.Visible = true;
	else
		PetTemplate.EquipMarker.Visible = false;
	end;
	p6:WaitForChild("TotalXP"):GetPropertyChangedSignal("Value"):Connect(function()
		PetTemplate.LevelLabel.Text = "Lvl. " .. getLevel(p6:WaitForChild("TotalXP").Value);
	end);
	local v33, v34, v35 = pairs(InventoryFrame:GetChildren());
	while true do
		local v36, v37 = v33(v34, v35);
		if v36 then

		else
			break;
		end;
		v35 = v36;
		if not v37:IsA("UIGridLayout") then
			v37.LayoutOrder = getLayoutOrder(v37.PetID.Value);
		end;	
	end;
end;
for v38, v39 in pairs(pets:GetChildren()) do
	newUI(v39);
end;
local StorageDisplay = script.Parent.MainFrame.StorageDisplay.TextLabel;
local DataFolder = localplayer:WaitForChild("Data");
local EquippedDisplay = script.Parent.MainFrame.EquippedDisplay.TextLabel;
pets.ChildAdded:Connect(function(p7)
	local v40 = 0;
	for v41, v42 in pairs(pets:GetChildren()) do
		if v42:WaitForChild("Equipped").Value == true then
			v40 = v40 + 1;
		end;
	end;
	StorageDisplay.Text = #pets:GetChildren() .. "/" .. DataFolder.MaxStorage.Value;
	EquippedDisplay.Text = v40 .. "/" .. DataFolder.MaxEquip.Value;
	newUI(p7);
end);
pets.ChildRemoved:Connect(function(p8)
	for v43, v44 in pairs(InventoryFrame:GetChildren()) do
		if not v44:IsA("UIGridLayout") and v44.PetID.Value == p8.PetID.Value then
			v44:Destroy();
		end;
	end;
	local v45 = 0;
	for v46, v47 in pairs(pets:GetChildren()) do
		if v47.Equipped.Value == true then
			v45 = v45 + 1;
		end;
	end;
	StorageDisplay.Text = #pets:GetChildren() .. "/" .. DataFolder.MaxStorage.Value;
	EquippedDisplay.Text = v45 .. "/" .. DataFolder.MaxEquip.Value;
end);
InventoryFrame.ChildAdded:Connect(function()
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
InventoryFrame.ChildRemoved:Connect(function()
	InventoryFrame.CanvasSize = UDim2.new(0, 0, 0, InventoryFrame.UIGridLayout.AbsoluteContentSize.Y + 200);
end);
local v48 = 0;
for v49, v50 in pairs(pets:GetChildren()) do
	if v50.Equipped.Value == true then
		v48 = v48 + 1;
	end;
end;
DataFolder.MaxStorage:GetPropertyChangedSignal("Value"):Connect(function()
	StorageDisplay.Text = #pets:GetChildren() .. "/" .. DataFolder.MaxStorage.Value;
end);
DataFolder.MaxEquip:GetPropertyChangedSignal("Value"):Connect(function()
	EquippedDisplay.Text = v48 .. "/" .. DataFolder.MaxEquip.Value;
end);
StorageDisplay.Text = #pets:GetChildren() .. "/" .. DataFolder.MaxStorage.Value;
EquippedDisplay.Text = v48 .. "/" .. DataFolder.MaxEquip.Value;

Now the sorting order seems to just sort a few pets properly then making others just randomly sorted.

I’m pretty confused on the way this lambda works but here’s what I’ve got

local PetTable = { 
		{petPower = SortFactor1};
		{petPower = SortFactor1};
		{petPower = SortFactor1};
	}

	table.sort(PetTable, function(a, b)
		return a.petPower > b.petPower 
	end)

When equipping the pets the order gets a lot less random and just relies on rarity, which is the sorting system I was sort of trying to avoid as it got confusing to find pets pretty fast.

I’m not sure I understand.

The lambda specified is sent two elements from the table, a and b. It returns an expression which is evaluated to a true/false value, which determines if the current element should be sorted to the top of the list. So in that example, pets with higher pet power should be found at the start of the list, ending with weaker ones.

You can change it to fit whatever type of sort criteria you’re looking for.