Layout order largest to smallest

Using the ‘Layout Order’ property, How can I arrange items in the order of largest to smallest? eg (540, 320, 77, 10) rather then it being the other way around. I feel like the answer is pretty obvious but Its 1am and I cant think of one.

1 Like

Manual


You’ll need to go through each frame and change their layout order manually

Automation


The only way to flip it automatically, is to program a quick script to descending order by taking max layout order and removing current layout order.

Basically here is a quick code to do it for you:

for _, frame in pairs(scrollingframe:GetChildren()) do
    pcall(function()
        if frame["LayoutOrder"] then
            frame.LayoutOrder = #scrollingFrame:GetChildren() - frame.LayoutOrder
        end
    end)
end
1 Like

I’m kind of lost, manually would not be possible since it’s the players values I’m trying to sort haha but Im not sure I understand fully what your script is actually doing.

1 Like

Basically lets say you have a frame on 520 but there is 590 frames.

We then do this to find its descending order: 590 - 520 = 70

It’ll then mark it as the 70th frame which is its correct order for descending.

1 Like

Ohh yep I get it now thanks, I’ll give it a try in the morning and mark you as a solution.

1 Like

Another solution you can try if you’re struggling to understand the previous one is to use table.sort;

local myArray = ScrollingFrame:GetChildren()

--[[
    You can read this sort function as "if a's LayoutOrder is greater
    than b's LayoutOrder then a should come before b."
]]
table.sort(myArray, function(a, b)
    if not a:IsA("GuiObject") then -- making sure our instance has "LayoutOrder"
        return false -- if no layoutorder, send to the end
    end
    return a.LayoutOrder > b.LayoutOrder
end)

--[[
    Now that we've sorted the array and we know it is in order from largest
    to smallest, we can assign each object a new LayoutOrder to apply it to
    the UIListLayout.
]]
for index, value in myArray do
    if myArray[index]:IsA("GuiObject") then
        myArray[index].LayoutOrder = index
    end
end

table.sort()
have it returns a > b or b > a to sort from largest to smallest and smallest to largest

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.