Sort hierarchy by alphabetical order ?

Hello guys, is there a way to sort the elements of the hierarchy tab by alphabetical order ?
Because as soon as you duplicate an element it is no longer sorted correctly.
Help would be greatly appreciated :slight_smile:
THX

There is no sort command for the hierarchy view, unfortunately.

The hierarchy is automatically sorted alphabetically. If you have an object called “A” it will be at the top of the list, and if you rename it “Z” it will be at the bottom.

–Eric

Hello guys!
To Eric5h5, nope it doesn’t systematically sort itself correctly with duplicated elements, we have hundreds of assets here and it gets rapidly messy and difficult to go through elements for debugging phase.
Hope Unity 3.0 will come up with something more advanced.
To andeeee: thx a lot for your reply, really appreciate your quick and straight to the point replies on this forum. Keep it up!

Try this C# script, which adds a menu item called GameObject/Sort Children:

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class SortChildren : ScriptableObject {
	
	[MenuItem ("GameObject/Sort Children")]
	
	static void MenuAddChild() {
		Sort(Selection.activeTransform);
	}
	
	static void Sort(Transform current) {
		foreach (Transform child in current)
			Sort(child);
		current.parent = current.parent;
	}
}

Select an object and choose this menu item to alphabetically sort all of its children.

OK, old thread, i Know. Its still relevant.

In 4.3.4 my hierarchy was automatically Alphabetically sorted. Upgrading to 4.5 has left the hierarchy a total chaotic random mess. Have some sorting options been added? If not, what’s with the Auto-sort removal?

See Unity 4.5 New Hierarchy Window sorting - Unity Engine - Unity Discussions

1 Like

Thank you very much for the link :slight_smile:

I made a tool to address this very problem. You can find it over here.

1 Like

Thanks for the tool, exactly what I was looking for! @IsaiahKelly

Enable Alpha Numeric Sorting

FYI, this no longer works in 2017.3.

@ryan-m-amos Checking “Enable Alpha Numeric Sorting” does not turn the option on by default. You actually have to go to the Hierarchy itself afterwards and select the display option from the new icon in the search bar.

3402036--267839--hierarchy_sorting_mode.png

However, please be aware that you will not be able to sort UI elements, and therefore their draw order, when in alphabetical sorting mode. So you’ll probably want to go back to transform sorting when working with UI elements.

As of now none of the above code or links work. For anyone coming here in 2021, the below works.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class EditorTools : ScriptableObject
{
    [MenuItem("GameObject/Sort Children")]
    public static void MenuAddChild()
    {
        Sort(Selection.activeTransform);
    }

    static void Sort(Transform current)
    {
        List<Transform> children = new List<Transform>();
        foreach (Transform child in current)
            children.Add(child);
        children = children.OrderBy(o => o.name).ToList();

        foreach(Transform child in children)
        {
            child.parent = null;
        }

        foreach (Transform child in children)
        {
            child.parent = current;
        }
    }
}
3 Likes

@scorpioservo Thanks! I’ve improved the code to check for null, support Undo and do the LINQ query in one-line (optional, you can keep using the multi-line version for readability).

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class EditorSortChildren : ScriptableObject
{
    [MenuItem("GameObject/Sort Children")]
    public static void SortActiveTransformChildren()
    {
        if (Selection.activeTransform)
        {
            Sort(Selection.activeTransform);
        }
        else
        {
            Debug.LogErrorFormat("No game object selected in Hierarchy");
        }
    }

    private static void Sort(Transform current)
    {
        IOrderedEnumerable<Transform> orderedChildren = current.Cast<Transform>().OrderBy(tr => tr.name);

        foreach (Transform child in orderedChildren)
        {
            Undo.SetTransformParent(child, null, "Reorder children");
            Undo.SetTransformParent(child, current, "Reorder children");
        }
    }
}

I don’t think this will change much, but in case I improve the script further and forget to post updates here, you can see the latest version at:
https://bitbucket.org/hsandt/unity-commons-editor/src/develop/EditorSortChildren.cs

1 Like

This script works on normal gameobjects but breaks on Canvases. It changes the rectTransform Z value of each child element to what looks like a random value.

1 Like

Thanks.