I want to add items to my shop GUI which uses a UIGridLayout. The problem is that I organize my items by price so if I want to add items in the middle, I would need to take everything out and place them back in their new order…
I was just wondering if there is a way to move/sort items without having to take everything out and putting them back again that is faster. Also, I am aware of “Sort by Name” and I would like to know if there is an alternative.
local items = { {item1, 300}, {item2, 100} } -- [1] = item name, [2] = item price
-- Sort the table from least to greatest
table.sort(items, function(a, b)
return a[2]< b[2]-- returns least to greatest
end)
-- go through the items in the UI:
for i, v in ipairs(items) do -- ipairs starts at index pos 1 -> #table length
local itemButton = shop:FindFirstChild(v[1])
if (itemButton) then
itemButton.LayoutOrder = i
else
-- create ui element and set layout order to i
end
end
-- Now that this is done you would do
shop.UIListLayout:ApplyLayout()