• 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
0
Question by instruct9r · Nov 07, 2015 at 06:18 PM · listnamesortmatchordering

Sort List by another List Values - Different Type

Hello. I am trying to Sort a List to another List, which is of different type...

Explanation. I have 1 list with GameObjects and another List (Same Count), with the same GameObject's Names. The list with the names is reordered at some point. After that i want to sort the List with GameObjects to the List with the Names

Example

 List<GameObject> myObjects = new List<GameObject>();
 List<string> myObjectsNames = new List<string>();


 void SomeFunction()
 {
     GameObject[] curSelection = Selection.gameObjects;

     for (int i = 0; i < curSelection.Length; i++)
     {
         myObjects.Add(curSelection[i]);                // Store the Selected GO to the list
         myObjectsNames.Add(curSelection[i].name);    // Store the Selected GO's names to the list
     }

     // At some point, the List "myObjectsNames" will be sorted in different way. All the time both Lists will have the same Count.

     // After that... How can i sort myObjects List to be the same order as the myObjectsNames??

     // I can use myObjects[#].name to Compare it to myObjectsNames, to get a match...

     // I can do that with loop and 3rd List, but i was wondering if there is a better / faster way of doing it...

     // EXAMPLE with Loop
     List<GameObject> reorderedMyObjects = new List<GameObject>();

     for (int a = 0; a < myObjectsNames.Count; a++)    // Loop through Names
     {
         for (int z = 0; z < myObjects.Count; z++)        // Loop through Objects
         {
             if (myObjectsNames[a] == myObjects[z].name)    // Check if the names match
                 reorderedMyObjects.Add (myObjects[z]);    // Add the matched object to the 3rd List.
         }
     }

     // This loop works, but i have to assign the sorted gameObjects to third list. I want just to re-order the initial myObjects List
                           
 }
Comment
Add comment · Show 3
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 rageingnonsense · Nov 07, 2015 at 10:13 PM 0
Share

Have you considered just using a Dictionary?

avatar image HenryStrattonFW rageingnonsense · Nov 09, 2015 at 03:24 PM 0
Share

Based on the description of him re-organizing the list of names at elsewhere in his app. I'm guessing its more about the reordering of those names in some way ins$$anonymous$$d of just having a map of names to objects. Otherwise yes a dictionary would be the way to go.

avatar image instruct9r rageingnonsense · Nov 09, 2015 at 05:14 PM 0
Share

Haven't tried, but i think i need a List exactly.

I've seen plenty of ways, for Reordering List. (By value, by number, etc), so i thought, that there might be a way of reordering List by matching the elements to another list.

$$anonymous$$aybe using System.Linq.. I'm not sure what exactly i can use...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HenryStrattonFW · Nov 09, 2015 at 03:14 PM

EDIT: Seems I missed in my skim reading the fact that your code already has this solution implemented. Sorry about that.

I would suggest creating a third list (during your re-sort) and then iterating over the names, each time finding the appropriate gameObject and popping it into your new list (thus resulting in a list of matching order).

 Code removed, already exists in question post.


An alternative might be removing the splitting of the list at all. Is there any particular reason that you are storing the names in a separate list. Could you not just have the one list of game-objects, and then wherever you are re-ordering the names list, you instead re-order this list of game objects as you still would have access to their .name property. Or is there a specific reason you need the names in a list of their own?

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 instruct9r · Nov 09, 2015 at 05:12 PM 0
Share

I am using Reorderable List to show the names of the GameObjects. But if i use one list, it shows the names like "UnityEngine.Cube" and i can't edit that. That's why i need second list with just the names, so i can edit them, to show only Cube.

When i reorder the Reorderable List, it re-arranges the List with the names. Thus i need to sort the List with the Objects to the List with the names...

Haven't tried with Dictionary. That might be the way of doing it with one List (Dictionary), but i'm not sure if Reorderable List supports Dictionaries..

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 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 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

A node in a childnode? 1 Answer

Sorting a jagged list based on 2nd entry 1 Answer

How to add and sort a list of gameobjects by tag? 2 Answers

Sort list with non-anonymous delegate? 2 Answers

sort List alphabetically 2 Answers


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