I tried:
planes.Sort((x, y) => string.Compare(x.LastName, y.LastName));
planes is a List
I’m getting error on both LastName
Severity Code Description Project File Line Suppression State
Error CS1061 ‘GameObject’ does not contain a definition for ‘LastName’ and no extension method ‘LastName’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)
your error says
‘GameObject’ does not contain a definition for ‘LastName’ and no extension method ‘LastName’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive
This means x and y are gameObjects which do not have a “lastName” field or property. The Error is telling you its trying to find a field called Last name in the GameObjects but it can not find it, so it throws an error.
The variables and methods defined in the GameObject class is listed here
but I will first help you to sort any generic list.
List.Sort()
planes.Sort(delegate(GameObject x, GameObject y)
{
return string.Compare(x.name, y.name);
});
basically the same code you have, except i used a anonymous delegate method instead of a lambda. and i used name instead of LastName. if you added a script that contained a LastName Variable , then you can still use it to sort your list using the GetComponent Methods.
In Short, it wasn’t the comparing itself but the fact you tried to use a variable that didn’t exist.
Hope it helps