Sorting an array using Linq and OrderBy

I’m trying to sort an array of waypoint object each starting with ‘WP’ followed by a number from 1 - 9. The waypoints have a parent object which holds the script and each contain a spawnpoint object as a child. Each of the waypoints also have a ‘Waypoint’ script attached which is how I’m adding them to an array to begin with.

The code also gives the following error:

ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[UnityEngine.GameObject].Compare (UnityEngine.GameObject x, UnityEngine.GameObject y)
System.Linq.SortSequenceContext`2[Waypoint,UnityEngine.GameObject].Compare (Int32 first_index, Int32 second_index)
System.Linq.QuickSort`1[Waypoint].CompareItems (Int32 first_index, Int32 second_index)
System.Linq.QuickSort`1[Waypoint].MedianOfThree (Int32 left, Int32 right)
System.Linq.QuickSort`1[Waypoint].Sort (Int32 left, Int32 right)
System.Linq.QuickSort`1[Waypoint].PerformSort ()
System.Linq.QuickSort`1+<Sort>c__Iterator21[Waypoint].MoveNext ()
System.Collections.Generic.List`1[Waypoint].AddEnumerable (IEnumerable`1 enumerable)
System.Collections.Generic.List`1[Waypoint]..ctor (IEnumerable`1 collection)
System.Linq.Enumerable.ToArray[Waypoint] (IEnumerable`1 source)
WPGroup.Start () (at Assets/016 - Move Object Randomly with Radius/WPGroup.cs:17)

The code I am working with is as follows;

using System.Linq;

public Waypoint[] WPGroupList = new Waypoint[9];
public GameObject[] WPgoGroupList = new GameObject[9];
// Use this for initialization
void Start () 
    // Searches entire scene - just want to search this group
    //WPGroupList = GameObject.FindGameObjectsWithTag("Waypoint");

    // Pass all waypoints inside object into array
    WPGroupList = gameObject.GetComponentsInChildren<Waypoint>(); // Select(go => go.transform).ToArray();
    // Order array in ascending order - not working
    WPGroupList.OrderBy(go =>;       

    // Created array of GameObjects afterwards to test if problem was that it was 
    // having trouble because they were waypoints
    for (int i = 0; i < WPGroupList.Length; i++)
         // Copy complete gameObjects from WP list into this GameObject array
         WPgoGroupList _= WPGroupList*.gameObject;*_


// Try to sort that - not working

OrderBy is not a function that you call on a collection in place. I don’t know why it compiles for me; it doesn’t do anything. This is how LINQ works:

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

You have to assign back to the array, if you even indeed need an array:

WPGroupList = WPGroupList.OrderBy(go =>;   

Rename your variables, though. PascalCase is not for fields. Also, I can’t get your particular error to show up.

One issue with using Linq to sort with OrderBy, if you have more than 9 objects, say 1,2,3,4,5,6,7,8,9,10, Linq will sort them as 1,10,2,3,4,5,6,7,8,9 which is most likely not what you want.

The fix for this, is simply:

WPGroupList = WGroupList(go=>int.Parse(;

Where 9 represents the substring section of where you named your index number on your waypoint game object name (exe. Waypoint-9).

Hope that helps you further.