I want to transform this script to make the code sort using any kind of sorting algorithm in example quick sort, bubble sort, selection sort... etc.
Is there a way to replace
instantiatedObjects = instantiatedObjects.OrderBy(go => go.name).ToList();
and change sorting method to quick sort in example? Is it necessary to use arrays or while using list like this is it possible to make it work with quick sort and other sorting algorithms?
The script is briefly creates clone of the gameobjects at the scene then sort them, then it destroys the original gameobjects so the clones will the only ones at the scene that are sorted.
Here is my script;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets {
class Gameobjects : MonoBehaviour {
public Button s_YourButton;
[SerializeField]
private GameObject[] deck;
public List<GameObject> instantiatedObjects;
void Start() {
Button btn = s_YourButton.GetComponent<Button>();
//Calls the TaskOnClick method when you click the Button
btn.onClick.AddListener(TaskOnClick);
}
List<Vector3> vectorList = new List<Vector3>();
void TaskOnClick() {
Fill();
instantiatedObjects = instantiatedObjects.OrderBy(go => go.name).ToList();
for (int i = 0; i < instantiatedObjects.Count; 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);
}
}
public void Fill() {
vectorList.Clear();
instantiatedObjects = new List<GameObject>();
for (int i = 0; i < deck.Length; i++) {
GameObject spawnedObject = Instantiate(deck[i]) as GameObject;
instantiatedObjects.Add(spawnedObject);
vectorList.Add(spawnedObject.transform.position);
}
}
}
}