Editor Arry Sorting Script

I found myself often with the problem that i need to add tons of items into a Array.
Normally you expect so see the same order you selected the items.
But Unity does not follow the order.

So i tinker a scrip that sort a Array by name in the editor.
In the inspector of the scrip in which the Array is needed to sort a button will appear.
Tested with textures, but should work for the most other stuff. You can easily add all the data type you need.

ArraySort.cs

using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor( typeof(name_of_script_which_contains_the_array) )]
public class ArraySort : Editor {
     name_of_script_which_contains_the_array m_Instance;
     public override void OnInspectorGUI () {
          this.DrawDefaultInspector();
          if(GUILayout.Button("Sort the array by name"))
          {
               name_of_script_which_contains_the_array current_target = target as name_of_script_which_contains_the_array;
               Debug.Log("The Array name_of_sorted_array got sorted by Name.");
               System.Array.Sort( current_target.name_of_sorted_array, CompareObNames );
          }
     }

     int CompareObNames( Texture x, Texture y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( Texture2D x, Texture2D y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( GameObject x, GameObject y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( Transform x, Transform y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( GUIText x, GUIText y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( GUITexture x, GUITexture y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( Material x, Material y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( PhysicMaterial x, PhysicMaterial y )
     {
     return x.name.CompareTo( y.name );
     }
     int CompareObNames( ProceduralMaterial x, ProceduralMaterial y )
     {
     return x.name.CompareTo( y.name );
     }
}

Not Really smooth when i test the ArraySort.cs
No Button exist in Inspector but I move “DrawDefaultInspector()” below “GUILayout.Button…”
Even Inspector Button exist normally.
It’s still not work. When I click the Sorting Button the Array is sorted looks good. But Run project It’s back to original array(Not sorted).

Anyone Knew Whats happen?

Also try serializedObject.ApplyModifiedProperties() serializedObject.Update() . It’s not work too.

I retested it because i thought the upgrade from 3.2 to 4.3 could have broke it but it works fine for me(so far)(i tested it with 20 Different Materials).
I did a debug at runtime of the whole array to see if it correct at runtime and it was.

Could you give me an not working example ?
Of what type of content is your array?

I created it to import and sort textures but if you have a custom type you may have to add a new version of the “CompareObNames” function matching your type.

Thank you for your reply. Here is the code.
My Unity version is 4.0.0f7 . I’ll upgrade to new version to testing same code again.

GuiExplore .cs

using UnityEngine;
using System.Collections;

public class GuiExplore : MonoBehaviour {
	public Texture [] image;  // 90 textures drag in
	int   	index=-1;

	// Initial index when enable 
	void Start () {
		index = -1;	
	}
	
	void Update () {
        // change texture on GUI( as GUI background)
        // this object needs a GUITexture
	}
}

=====================================
GuiExploreEditor .cs

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(GuiExplore))]
public class GuiExploreEditor : Editor
{
	int SortByName( Texture o1, Texture o2)
	{
		return o1.name.CompareTo( o2.name);
	}
	
	public override void OnInspectorGUI()
	{
		if( GUILayout.Button("Editor: Sorting array"))
     		        System.Array.Sort( ((GuiExplore)target).image, SortByName);

		DrawDefaultInspector();
	}
}

Sorry! It’s Working fine at version 4.1.0f4.