Unity C# script: Array sorting according to an integer in another script

Guys I need some help for C# scripting.

I have an int objectNum in the Objective Completion script. In the ObjectiveSorter() method, I want to sort the objectiveScriptArr according to the objectNum int.
So, for eg, the Objective Completion script with objectNum = 2 will be at index 1 i.e., position 2 of the objectiveScriptArr.

Is it possible to do so?

public ObjectiveCompletion[] objectiveScriptArr;

void Awake ()
{
   objectiveScriptArr = (ObjectiveCompletion[]) FindObjectsOfType(typeof(ObjectiveCompletion));
   //This randomly collects the script without considering the 'ObjectiveNum' int in 'ObjectiveCompletion' script
}

void ObjectiveSorter ()
{
   foreach(ObjectiveCompletion objective in objectiveScriptArr)
   {
       int objectiveIndex;
   }
}

For future reference, to post code to these forums, please use the </> button to create a region of pre-formatted text to paste the code into.

C# arrays allow you to specify a custom “compare” method to be used when comparing elements of an array. This is described here:

and here:

In your case, first you will need to expose the objectNum on a public interface so that other scripts can access it. The easiest way to do this is just to make the variable public.

public class ObjectiveCompletion : MonoBehaviour
{
    public int objectNum;
    ...
}

Then in your script which will be doing the sorting, you will need to define a class with a IComparer interface so that you can define a custom compare function to be used when sorting an array of ObjectiveCompletion objects. You should be able to do this in the same file that you need to do the sorting.

Once the custom comparer is defined, you can pass this into the Array.Sort function to sort the array based on the rules you defined. I tested the below code in a simple scene and it behaved as I expected; the array got sorted based on the objectNum value.

using System.Collections;
using UnityEngine;
using System;  //You need to add System to get the Array Sort method

public class ObjectiveCompare : IComparer
{
    public int Compare(object x, object y)
    {
        //Since this operates on generic "objects" we must cast the input
        //parameters to the type that we want.
        int first = ((ObjectiveCompletion)x).objectNum;
        int second = ((ObjectiveCompletion)y).objectNum;
        return first.CompareTo(second); //This is the normal integer comparison
    }
}

public class SortSyntax : MonoBehaviour
{
    void Start()
    {
        ObjectiveCompletion[] allObjectives = FindObjectsOfType<ObjectiveCompletion>();
        Debug.Log("List before sorting");
        PrintInfo(allObjectives);

        //Create a new instance of the comparison class that you defined above...
        IComparer comparer = new ObjectiveCompare();
        //...and pass it into the Array Sort function
        Array.Sort(allObjectives, comparer);

        Debug.Log("List after sorting");
        PrintInfo(allObjectives);
    }

    private void PrintInfo(ObjectiveCompletion[] Objectives)
    {
        foreach(ObjectiveCompletion obj in Objectives)
        {
            Debug.Log("name: " + obj.gameObject.name + " num: " + obj.objectNum);
        }
    }
}

Thanks a lot. I’ll keep the things you said in mind