- Home /
Question by
Vordas32 · Dec 06, 2012 at 10:02 AM ·
gameobjecttagsort
Sort game objects by tag alphabetically
Hi to everybody,
I have 56 sphere in Game which I gave tag "Waypoint". So i have in my game Waypoint_1, Waypoint_2, Waypoint_3 ... I wanted to be sorted. From 1 to 56. Now Array look randomly.
This is a code so far.
import System;
var MainWaypoints:GameObject[];
var StoreWaypoints:Array = new Array();
function Awake()
{
MainWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");
for (var obj:GameObject in MainWaypoints)
{
StoreWaypoints.Add(obj);
}
StoreWaypoints.Sort();
print("Lenght: " + StoreWaypoints.length);
print(StoreWaypoints);
}
Every thing works great to the line when I execute Sort. I use new array becasue I thought the Sort will work. Now unity give me this error:
InvalidOperationException: `No IComparable or IComparable interface found.`
I realy need help how to sort game object from first number to last.
Comment
Answer by Landern · Dec 06, 2012 at 02:09 PM
You should use a List, but for the sake of answering you question.
Use Array.Sort with the default comparer.
import System;
var MainWaypoints:GameObject[];
var StoreWaypoints:Array = new Array();
function Awake()
{
MainWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");
StoreWaypoints = Array.Sort(MainWaypoints);
print("Lenght: " + StoreWaypoints.length);
print(StoreWaypoints);
}
For your reference(please use the search feature), same question answered, another one.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085034im_/https://answers.unity.com/themes/thub/images/avi.jpg)