How to sort a built-in array in the inspector?

I’ve been having this problem for quite a long time now, and I’m getting tired of trying to put together workarounds that don’t even work. I’ve also been able to find this question posted a few other times in the past, but none of them have an absolute answer.

My problem is that when I lock the inspector and drag over a large selection of numbered items from the project pane to populate a built-in array I have, it adds all of them in a random order, very very far from the order which they’re numbered in.

For example - a large quantity of textures with numerically ordered file names such as “img_001, img_002, img_003, img_004, etc.” will become scrambled once dragged into an array in the inspector, leaving them a mess and in no order whatsoever.

Now my question is - can I reorganize all of them into numerical order in the inspector?

Dragging them over one-by-one or individually dragging them around inside the inspector is not an option, as I have a total of 1,071 numbered textures I need to populate an array with. Now, as I’ve already said, I’ve been able to find this question asked by a few other people. Here, here and here. However, none of them had a straightforward answer. The script provided in the second link is not an editor script, and my script that holds and uses these textures is written in JS, so the possibility of me successfully converting that C# script to JS and implementing it into my own script and it actually working is pretty much non-existent. I know almost nothing when it comes to C#, and I get all kinds of errors that I can’t even understand. The script found in the third link is really only part of a script, and as much as I hate asking for something like this without providing my own rough draft to improve upon, I just can’t get this working no matter how many different ways I go about it. It seems pretty ridiculous to me that there isn’t already a built-in option/button for this, and I’m getting extremely frustrated over the lack of information I’ve been able to find on it. I’ve never made an editor script, let alone a custom inspector. Could someone please help me figure out a way to quickly sort things by name in the inspector? Thanks in advance.

A slightly crude, but simple way:

@script ExecuteInEditMode()

var textures : Texture2D[];

function OnEnable () {
	System.Array.Sort (textures, TexNameSort);
}

function TexNameSort (a : Texture2D, b : Texture2D) : int {
	return a.name.CompareTo (b.name);
}

So whenever OnEnable is called in edit mode, the textures array is sorted by name.

FWIW, in case somebody else stumbles across this question as I did, here’s the solution I have settled on for now. In the script containing an array property, add a bit of code to create a contextual menu, like this:

[ContextMenu ("Sort Frames by Name")]
void DoSortFrames() {
    System.Array.Sort(frames, (a,b) => a.name.CompareTo(b.name));
    Debug.Log(gameObject.name + ".frames have been sorted alphabetically.");
}

In this example, I have a “public Sprite frames” property; just change all occurrences of “frames” to whatever your own array property is, and it ought to work. (Also, this is C#; changes would be needed for JavaScript.)

This lets the designer decide when they want to sort, by right-clicking the script and choosing the new “Sort” command the above code adds to the contextual menu.