0

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.

Beginning, Randomized, 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);
            }
        }
    }
}
  • 2
    LINQ-to-objects [already uses a variation of Quick Sort](https://stackoverflow.com/questions/2792074/what-sorting-algorithm-is-used-by-linq-orderby). – Phylogenesis May 24 '18 at 13:40
  • What about Bubble sort and Selection sort? I need to make this work by other kind of sorting algorithms. – HumaNSLayeR May 24 '18 at 13:45
  • @HumaNSLayeR Why? Bubble sort is notoriously one of the worst sorting algorithms. – juharr May 24 '18 at 13:46
  • Well it does'nt matter which sorting algorithm is it, it can be any, I have a project work for it so it needs to use different kind of sorting algorithms to sort the balls. – HumaNSLayeR May 24 '18 at 13:48
  • Then you will have to implement your own version of `OrderBy()` function that takes a sorting algorithm as a parameter. The standard LINQ library only allows you to have a custom comparer. – Phylogenesis May 24 '18 at 13:49
  • So I have to change OrderBy method manually? – HumaNSLayeR May 24 '18 at 13:57
  • @HumaNSLayeR No, you have to write your own method that will order the collection with whatever algorithm you choose. – juharr May 24 '18 at 13:59
  • You need to implement your desired algorithm yourself. –  May 24 '18 at 14:07

0 Answers0