How do I sort Game Objects from closest to furthest, by tags?

I am working on a music game and I have an editor. I place notes down that follow a song. These notes all contain the information to be created in their name, so I need to write a file with their names. I need their names written in order from closest to furthest away, because it will improve the performance of my game. Now the question:

Does anyone know a way to iterate through a loop that will sort these Game Objects from closest to furthest? You should note that I am using findwithtag to find them all. I considered using a list, but I cant think of an efficient way to do that. Does anyone know a cost effective (in processing time) way of doing this? Please and Thank You!

P.S. psuedo works great for me too. Possibly better, I like to learn.

Well, your code contains two problems :wink:

First, the Array class. It’s slow but the worst thing is it’s not type safe. You store GameObjects in this container. A GameObject can’t be compared to another GameObject. You want to compare the GameObject “names” which are strings.

So my advice is: Use a List instead of Array and store the names in the list and not the Gameobjects:

var Notes = GameObject.FindGameObjectsWithTag ("Note");
var Names = new List.<String>(Notes.length); // set initial length for better performance
for (var Note in Notes) {
    Names.Add(;  // store the name of the gameobject

edit To use the generic list you have to import the System.Collections.Generic namespace at the top of your script:

import System.Collections.Generic;