Insert a new GuiObject in UIGridLayout?

Hi there!

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.

Thanks in advance! :smiley:

1 Like

You could do something like this:

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()
1 Like

Epic! This is literally exactly what I needed, thank you very much for your help! :smiley:

1 Like