GuiObject:GetChildrenByLayoutOrder()

I feel like this should be a thing but it isn’t. Currently you can do it with

local c = guiObject:GetChildren()
table.sort(c, function (a, b)
  return a.LayoutOrder < b.LayoutOrder
end)

but it would be a nice little API addition regardless.

4 Likes

Do you have a usecase in mind for this?

I sure did! It’s escaped my mind for the time being.

In all my scripting days I did not know you could provide a function to table.sort.

1 Like

Strictly speaking, this example code won’t produce the same ordering as a layout will, because table.sort doesn’t perform a stable sort (in most implementations, it is quicksort, which has the further disadvantage of hitting the worst-case O(N^2) run time for an already sorted array). You’d have to use a stable sort algorithm like merge sort in order for it to produce the same result. This is fairly relevant because if all your children have LayoutOrder == 0, table.sort will put them into semi-random order, whereas a stable sort will maintain child/insertion order.

1 Like

This is partially why I wanted a method. A use case I did find myself wanting was in conjunction with UIPageLayout, I may want to number each page and right now that is tricky for pages with the same order. Another is that I may want to tint every other element so I need to know their display order.