- Home /
sort array of transforms by name
Hi, I've been trying to sort a list of transforms by name in c# to no avail. I've read the 'sort' documentation, but I can't get it to work. I also can't get CompareTo to work in any way, even though i've added System.Collections.Generic.
I would greatly appreciate an example.
Thanks, -Juanelo
Answer by Bunny83 · Jul 24, 2011 at 01:45 AM
I guess the easiest way is to use the Comparison version of Sort().
http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx
You just need to define a method that takes two references of the type you want to compare and that returns an integer.
The Comparison delegate:
http://msdn.microsoft.com/en-us/library/tfakywbh.aspx
public class Example : MonoBehaviour
{
public List<Transform> myList;
private static int CompareTransform(Transform A, Transform B)
{
return A.name.CompareTo(B.name);
}
public void SortList()
{
myList.Sort(CompareTransform);
}
[...]
}
That should do what you want, haven't tested it ...
Thank you Bunny, I'll give that a shot. I really appreciate your help.
Answer by Chuckalicious · Jul 27, 2015 at 06:59 AM
I've seen the same issue. Bunny what you have is basically the same as:
using System.Linq;
_myList.Sort((x, y) => x.name.CompareTo(y.name));
However, this still does not seem to sort correctly. I have a list of transforms all named Tile_1, Tile_2, Tile_3, ...
I still can get this to sort in order of 1, 2, 3, 4,....
What gives?
Your answer
