Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by nuverian · Dec 26, 2012 at 06:28 PM · arraylistsort

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 :-)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Setzer22 · Dec 26, 2012 at 06:45 PM 0
Share

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

avatar image nuverian · Dec 27, 2012 at 02:18 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

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();
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nuverian · Dec 27, 2012 at 02:15 AM 0
Share

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.

avatar image nuverian · Dec 27, 2012 at 02:26 AM 0
Share

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 :-)

avatar image whydoidoit · Dec 27, 2012 at 07:41 AM 0
Share

Hey sorry I missed your comments - I wrote an article on Linq in Unity on Unity Gems

avatar image naglers · Aug 22, 2013 at 07:18 PM 0
Share

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();

avatar image whydoidoit · Aug 22, 2013 at 07:32 PM 1
Share

BTW You could actually do:

    $$anonymous$$yList = GetComponentsInChildren<Whatever>().OrderBy(go=>go.name).ToList();
Show more comments
avatar image
4

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
   

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nuverian · Dec 27, 2012 at 02:16 AM 0
Share

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

avatar image Eric5h5 · Dec 27, 2012 at 02:49 AM 0
Share

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);".

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nuverian · Dec 27, 2012 at 02:28 AM 0
Share

Thanks for answer. I will just go with the Linq version as being quite shorter and I like it short :-)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges