How can i sort a List ?
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?)
Answer by Fornoreason1000 · Dec 04, 2016 at 03:05 AM
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
Your answer
Follow this Question
Related Questions
How can I check if a object in the hierarchy have been destroyed using EditorWindow type script ? 1 Answer
The laser problems 0 Answers
How do i put "Wasted!" after respawn? 0 Answers
Reverse memcpy for NativeArray? 0 Answers
How can i make sure the script will work only on the attached object ? 1 Answer