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)

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 :wink: