sort children components using siblingIndex inside a for loop

I have created a method to sort cards by Rank (ACE… KING). The method depicts insertion sort algorithm and seems to work fine. I have a button somewhere in the screen and upon clicking this button, this method would be called so that the cards on hand would be sorted by Rank.


Current behavior : only one card is sorted at one time relative to other cards a/c to rank.


Expected behavior : all cards should be sorted at once when I click the button but this is not happening.


I am kind of new with Unity. So, Can anyone help me achieve the behavior I am looking for. Thanks in advance !


public IEnumerator SortByRank(Transform[] cardGameObjects) {
        int i, j, min_j;

        for (i = 1; i < cardGameObjects.Length; i++) {
            min_j = i;

            for (j = i + 1; j < cardGameObjects.Length; j++) {
                if (cardGameObjects[j].GetComponent<Card>().rank < 
                    cardGameObjects[min_j].GetComponent<Card>().rank) {
                    min_j = j;
                }
            }

            cardGameObjects*.SetSiblingIndex(cardGameObjects[min_j].GetSiblingIndex());*

yield return null;
}
}

Code NOT TESTED

public IEnumerator SortByRank(Transform[] cardGameObjects)
{
   Card[] cards = System.Array.ConvertAll(cardGameObjects, go => go.GetComponent<Card>());
   System.Array.Sort(cards, (card1, card2) => card2.rank - card1.rank);
   for(int i = 0 ; i < cards.Length ; ++i)
   {
        cards*.transform.SetSiblingIndex(i);*

yield return null;
}
}