- Home /
How to sort a list of gameobjects by their name?
Hey all.
As the subject states, I have a simple list of type GameObject, and I'd like to sort the list based on the gameobject's names in there. Actually all of those gameobject names have a prefix of a number and underscore (eg: 23_***).
I couldnt get my head about how its done quickly.
Thanks :-)
Have you tried the Sort method for the generic list?
just call it like this:
yourList.Sort();
This might not work and you might need to use something a bit more complex (that's why i'm not posting this as an answer) but it's worth trying
Hey thanks for comment. I did some "tests" but my biggest issue at sorting this list out, is that I need to sort them based on the numerical prefix the names have.
Answer by whydoidoit · Dec 26, 2012 at 09:34 PM
You can use Linq by adding
CS:
using Linq;
JS:
import Linq;
Then all you need to do is:
CS
var sortedList = unsortedList.OrderBy(go=>go.name).ToList();
JS
var sortedList = unsortedList.OrderBy(function(go) go.name).ToList();
Hi and thanks. Is there any trick that I can do so that the list is not sorted like: 1,10,11,12,2,3,4 etc?, but rather in order. As stated in my post, the names have a prefix in them. Thanks.
Ok never$$anonymous$$g. I got it working. The truth is that each element was of a custom class, that each one was assigned to a GameObject. So I did ..OrderBy(function(foo) foo.orderInt).ToList(); I am marking this as the correct answer since is what I used at the end. Thanks everyone :-)
Hey sorry I missed your comments - I wrote an article on Linq in Unity on Unity Gems
Thank you so much, this is definitely the way to go. I only wanted one list though so all I did was this in C# public list $$anonymous$$yList; foreach(element E in GetComponentsInChildren()) { $$anonymous$$yList.Add(E); } $$anonymous$$yList = $$anonymous$$yList.OrderBy(go=>go.name).ToList();
BTW You could actually do:
$$anonymous$$yList = GetComponentsInChildren<Whatever>().OrderBy(go=>go.name).ToList();
Answer by Yokimato · Dec 26, 2012 at 07:05 PM
You will want to make a comparison which can be seen from MSDN documentation Here Since there is already a comparison for strings yours needs onlyntoncall that like such:
private static int SortByName(GameObject o1, GameObject o2) {
return o1.name.CompareTo(o2.name);
}
// later on...
myObjects.Sort(SortByName); // sorted by name now
I tried this and I dont know if its something I didnt understand or the fact that Im under UnityScript rather than C#, allthough I think the same should work. Obviously, The line: myObjects.Sort(SortByName); expects the "SortByName" to be provided by the arguments it requires (o1, o2). I dont really understand what I am supposed to do :/ Thanks
Unityscript is:
static function SortByName (o1 : GameObject, o2 : GameObject) {
return o1.name.CompareTo (o2.name);
}
It's used in the same way, namely "myObjects.Sort(SortByName);".
Answer by MaGuSware™ · Dec 26, 2012 at 07:40 PM
Maybe something like this will help.
note: The GameObject after List needs to be wrapped in angle brackets.
List GameObject unsortedObjects; //<-- your objects
List GameObject sortedObjects = new List GameObject();
bool added = false;
//Iterate over all objects in the unsorted
for(int i = 0; i < unsortedObjects.Count; i++)
{
added = false;
//Iterate over the objects in sorted for comparison.
for (int j = 0; j < sortedObjects.Count; j++)
{
//The condition, in this case, name being less then
if (unsortedObjects[i].name.CompareTo(sortedObjects[j].name) < 0)
{
//Insert the object into list at point. Placing it before.
sortedObjects.Insert(j, unsortedObjects[i]);
added = true;
break;
}
}
//Add to back of list
if(!added)
sortedObjects.Add(unsortedObjects[i]);
}
//if you want it as an array
GameObject[] sortedObjectsArray = sortedObjects.ToArray();
I don't know how the Sort() works that Yokimato has mentioned but this method should be quite fast regardless.
Thanks for answer. I will just go with the Linq version as being quite shorter and I like it short :-)
Answer by naglers · Aug 23, 2013 at 09:06 PM
by default the .Sort() function sorts the objects by instance ID so unless you created them in your scene in the proper order when they were initially
Your answer
Follow this Question
Related Questions
Sorting Variables Help 1 Answer
A node in a childnode? 1 Answer
[C#] Sorting a List of Gameobjects Alphabetically 2 Answers
Sorting a list by distance to an object? 1 Answer
How to Sort Multiple Vector Arrays in order of Values 1 Answer