Sorting the children of a Grid Layout in 4.6 UI

Hi. So I’m making a quest log. Once I quest is started, I need to add it to the active section of the quest log, which is a Grid Layout. The only problem is, this adds it at the bottom of the list. I want it to be added to the top so the most recent quests appear first. How can I arrange the elements of the grid layout through script? Thanks.

You need to de-parent your ui elements, sort them (in a List or Array) and then re-parent them to the GridLayout transform in the sorted order. Remember that for UI elements you need to use Transform.SetParent to set the parenting.

There’s a solution that’s better performant than de-parenting the UI elements and then parenting them again in the required order, which is using the SetSibling methods from the Transform.

SetAsFirstSibling: Move the transform to the start of the local transform list.
SetAsLastSibling: Move the transform to the end of the local transform list.
SetSiblingIndex: Sets the sibling index.

So, in your case, when a quest becomes active you just need to set that quest object as the first sibling.
You can find more info in this thread:

It depends on what the object holding the list of quests is, but if it’s just a plain List why not simply use list.Insert(0, item) when you add it to put it first?