How can I sort a list of GameObject by name?
I tried looking into the documentation and ust cannot find there information on syntax / parameters for a List Sort. (Very strange!)
Here is what I am trying to do:
playerCardTargets = new List<GameObject>(GameObject.FindGameObjectsWithTag("PlayerCardTarget"));
print("Count before: " +playerCardTargets.Count);
playerCardTargets.Sort();
print("Count after: " +playerCardTargets.Count);
I cannot find information on what parameters or other options Sort() takes. As well, although I get no errors pre-runtime, on runtime the game reports an error as soon as it hits the script: playerCardTargets.Sort();
All I really want to do is sort the list alphabetically by GameObject names. I come from a Flash Actionscript 3 background where we can do such Array sorts. Is there ANY simple way or do I have to code my own?
Here is the error:
ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[UnityEngine.GameObject].Compare (UnityEngine.GameObject x, UnityEngine.GameObject y) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)
System.Array.compare[GameObject] (UnityEngine.GameObject value1, UnityEngine.GameObject value2, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1745)
System.Array.qsort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1722)
System.Array.Sort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1675)
Rethrow as InvalidOperationException: The comparer threw an exception.
System.Array.Sort[GameObject,GameObject] (UnityEngine.GameObject[] keys, UnityEngine.GameObject[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1678)
System.Array.Sort[GameObject] (UnityEngine.GameObject[] array, Int32 index, Int32 length, IComparer`1 comparer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1623)
System.Collections.Generic.List`1[UnityEngine.GameObject].Sort () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:568)
GameController.Start () (at Assets/Scripts/GameController.cs:107)
Answer by Matt1000 · Mar 16, 2017 at 09:24 PM
Perhaphs this can help you. There is no point in rewriting more examples. The answer is not specifically what you need but the comments there are your answer. Good luck ;)
YES! I had come across that page before but it didn't "click" that it did apply to what I wanted. So in the end it was as simple as adding this method:
private static int CompareListByName(GameObject i1, GameObject i2)
{
return i1.name.CompareTo(i2.name);
}
And modifying the sort like this:
playerCardTargets.Sort(CompareListByName);
Your answer
Follow this Question
Related Questions
Sort list by distance AND by being active 3 Answers
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 2 Answers
How I Sort a List at Start?,Sorting a List on Start 0 Answers
Sort a list of class by a vector variable in the class 2 Answers
How can I check value stored in a list when pressing button with the same name? 2 Answers