- Home /
 
Illustrating Sorting Process of Balls In Unity Using Sorting Algorithms
I want to illustrate the process of sorting by two by two replacing balls. Like in this image;
but with Balls, can you help me to do it?
There should be some delay inside the loop for Illustrating the process but I couldn't make it properly and when I click button it sortes like this; 
 After randomizing the faces of the randomized balls are 5 3 2 4 1 so it should sort 5 - 3 and show that sorting then sort 2 - 5 then 4 - 5 so that replacing needs to be illustrated.
I have other sorting algorithms such as Selection and Bubble but I can do them myself I suppose if I can get answer to this one.
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 namespace Assets
 {
     class Gameobjects : MonoBehaviour
 {
     public Button s_YourButton;
     [SerializeField]
     public GameObject[] Balls = new GameObject[5];
  
     
     public GameObject[] instantiatedObjects= new GameObject[5];
     void Start()
     {
         Button btn = s_YourButton.GetComponent<Button>();
         //Calls the TaskOnClick method when you click the Button
         btn.onClick.AddListener(TaskOnClick);
         Balls[0] = GameObject.Find("5");
         Balls[1] = GameObject.Find("3");
         Balls[2] = GameObject.Find("2");
         Balls[3] = GameObject.Find("4");
         Balls[4] = GameObject.Find("1");
         //performInsertionSort(instantiatedObjects); 
     }
     List<Vector3> vectorList = new List<Vector3>();
     void TaskOnClick()
     {
         Fill();
         //instantiatedObjects = instantiatedObjects.OrderBy(go => go.name).ToList();
         performInsertionSort(instantiatedObjects);
         //for (int i = 0; i < instantiatedObjects.Length; i++)
         //{
         //   instantiatedObjects[i].transform.position = vectorList[i];
         //}
         string name = "1";
         string name1 = "2";
         string name2 = "3";
         string name3 = "4";
         string name4 = "5";
         GameObject go1 = GameObject.Find(name);
         GameObject go2 = GameObject.Find(name1);
         GameObject go3 = GameObject.Find(name2);
         GameObject go4 = GameObject.Find(name3);
         GameObject go5 = GameObject.Find(name4);
         //if the tree exist then destroy it
         if (go1 & go2 & go3 & go4 & go5)
         {
             Destroy(go1.gameObject);
             Destroy(go2.gameObject);
             Destroy(go3.gameObject);
             Destroy(go4.gameObject);
             Destroy(go5.gameObject);
       
         }
     }
     private void performInsertionSort(GameObject[] instantiatedObjects)
     {
         {
             int k = 0;
             for (int i = 0; i < instantiatedObjects.Length - 1; i++)
             {
                 int j = i + 1;
                 while (j > 0)
                 {
                     if (string.Compare(instantiatedObjects[j - 1].name, instantiatedObjects[j].name) > 0)
                     {
                         GameObject temp = instantiatedObjects[j - 1];
                         instantiatedObjects[j - 1] = instantiatedObjects[j];
                         instantiatedObjects[j] = temp;
                         instantiatedObjects[j].transform.position = vectorList[j];
                         }
  
                         j--;
                         
                     }
                     StartCoroutine(Example());
                 }
             }
         }       
     IEnumerator Example()
     {
         print(Time.time);
         yield return new WaitForSeconds(0.5f);
         print(Time.time);
     }
 public void Fill()
 {
     vectorList.Clear();
     instantiatedObjects = new GameObject[5];
     for (int i = 0; i < Balls.Length; i++)
     {
         GameObject spawnedObject = Instantiate(Balls[i]) as GameObject;
         instantiatedObjects[i] = spawnedObject ;
         vectorList.Add(spawnedObject.transform.position);
     }
 }
         }
     }
 
              
               Comment
              
 
               
              Your answer