• 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 smirlianos · Aug 17, 2015 at 08:56 AM · arraylistsortarrange

How to arrange list

Hello,

I have two lists:

 var PosList : List.<float>;
 var ObjList : List.<GameObjects>;

I want to arrange the first list, and the second list to be arranged accordingly to the first one.

For example:

 //list one contains:
 [3]
 [1]
 [2]
 
 //list two contains gameObjects with the names:
 [number3]
 [number1]
 [number2]
 
 //I sort the first list, so I use:
 PosList.Sort();
 
 //Now the first list became:
 [1]
 [2]
 [3]
 
 //How can I arrange the second list to mach the first one? //Like t$$anonymous$$s:
 [number1]
 [number2]
 [number3]


Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cjdev · Aug 17, 2015 at 09:30 AM

You could use some LINQ like t$$anonymous$$s:

 ObjList = ObjList.OrderBy(x => int.Parse(x.transform.name.Replace("number", ""))).ToList();
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
avatar image
0

Answer by NeverHopeless · Aug 17, 2015 at 09:38 AM

You should better use Dictionary<float, GameObject>. So once you call Sort, it will be sorted as per the key. Or you can try custom sorting procedures.

Also, there is a possibility to use SortedDictionary.

Using dictionary you don;t have to be precise about the value on each index, once the key sorted there corresponding value will be moved in the same way.

Hope you will not see t$$anonymous$$s problem then.

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
avatar image
0

Answer by stepan-stulov · Aug 17, 2015 at 01:20 PM

 [Serializable]
 public class PositionedGameObject
 {
     public GameObject gameObject;
     public float gameObjectPosition;
 }
 
 // Later...
 
 public List<PositionedGameObject> positionedGameObjects;
 
 // Event later...
 
 positionedGameObjects.Sort(ComparePositionedGameObjectByWhateves);


  1. What you have: you store a list of multi-purpose "complex" objects (a game object and its associated position) in a distributed manner in two lists w$$anonymous$$ch causes desyncing on top of being very unintuitive.

  2. What you actually want: create a class/struct that describes an object and its position together. Store a list of instances of that class/structure. Vuala, now try to have them unsynced. You can't.

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.

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

27 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

Related Questions

A node in a childnode? 1 Answer

[C#] Sorting a List of Gameobjects Alphabetically 2 Answers

Sorting a list by distance to an object? 2 Answers

How to Sort Multiple Vector Arrays in order of Values 1 Answer

List.Sort with IComparer 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