- Home /
Sorting an Array of GameObjects by Name
How can I sort an array of gameObjects by name in Javascript. I've looked at a few examples but they are concerning an array of values, not gameObjects. The names will have a number at the end which they will be sorted by in numerical order. (1, 2, 3, ect.) How can this be done?
Answer by tomekkie2 · Jun 27, 2012 at 12:01 AM
You can find the solution here
http://answers.unity3d.com/questions/155694/sort-list-alphabetically.html
If the starting string in the names are the same, like gameobject1, gameobject2, strip that with Substring(10), and compare the digital part only. If you have more than ten remember to use gameobject09 instead of gameobject9, because - say - 10 goes before 9 in alphabetical order.
You can also parse the digital part to int and sort numerically http://answers.unity3d.com/questions/146265/sort-array-by-ascending-numerical-order.html
or if the numbers are sequential just loop through the gameobjects
YourArray[int.TryParse (gameObject.name.Substring(10))] = gameObject
How can I make this go by numbers and not alphabetical order though?
Your answer
Follow this Question
Related Questions
Sort Array Of GameoObjects useing a variable 1 Answer
Order a GameObject Array 1 Answer
Access variables, objects in array in another script? 2 Answers
How to remove objects from a list ? 3 Answers
Can't add GameObjects to ArrayList 1 Answer