[UI] Setting hierarchy order of UI objects by script

Let’s say I have a UI grid layout, and it’s children are buttons. These are added during runtime from prefab buttons. How can I change in what order they are in the hierarchy?

Example:

Gridlayout
-Button A
-Button B
-Button C

now I want to put Button A in the middle, so it looks like this:

Gridlayout
-Button B
-Button A
-Button C

Look at this functions:

If you want to move a button one place to the bottom first get the index and then set it as index + 1.
If you know there’ll be only 3 buttons and you want a button at the middle, just set the index to 1.

I have had similar issues, SetAsLastSibling() worked for me!