Sort Gui objects by price

Hello devs,
How do i create a function that puts items from a gui in relation to the price of an item?

for example, in the print above, water should be the first item since it has the lowest value compared to other items, then it should be energy drink and so on.

I’m using module scripts to store information about items and then get their values ​​in a local script

Module Script
local FoodsFolder = game.ReplicatedStorage:WaitForChild("Foods")

local ShopModule = {
	["Water"] = {
		Name = "Water";
		Recuve = 5;
		Price = 10;
		Image = "rbxassetid://14502586173";
		
	};
	
	["Energy Drink"] = {
		Name = "Energy Drink";
		Recuve = 25;
		Price = 100;
		Image = "rbxassetid://14502719215";

	};
--- (...) ---

return ShopModule
Local Script

for i, RepFood in pairs(Foods:GetChildren()) do
	local ItemHandlerClone = script.ItemHandler:Clone()
	ItemHandlerClone.Parent = ScrollingItemsBackg.ScrollingFrame 
	ItemHandlerClone.Image = ShopModule[RepFood.Name].Image
	ItemHandlerClone.ItemName.Text = RepFood.Name
	
	InfoBackg.InfoName.Visible = true InfoBackg.PriceLabel.Visible = true InfoBackg.RecuveLabel.Visible = true InfoBackg.InfoImage.Visible = true
	BuyButton.Visible = true InfoBackg.Visible = true
	
	ItemHandlerClone.MouseButton1Click:Connect(function()
		SelectedItem = nil
		
		InfoBackg.InfoName.Text = ShopModule[RepFood.Name].Name
		InfoBackg.PriceLabel.Text = "$"..ShopModule[RepFood.Name].Price
		InfoBackg.RecuveLabel.Text = "+"..ShopModule[RepFood.Name].Recuve
		InfoBackg.InfoImage.Image = ShopModule[RepFood.Name].Image
		
		SelectedItem = InfoBackg.InfoName.Text
		
		BuyButton.MouseButton1Click:Connect(function()
			BuyFoodRE:FireServer(SelectedItem)
		end)
	end)
end

Try using table.sort and setting the LayoutOrder on the Items GuiObject to whatever table.sort makes it.

2 Likes