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
2
Question by cmpgk1024 · Nov 13, 2013 at 03:17 PM · c#arraysortlinqienumerable

Sorting an array of GameObjects by their position

I have an array of GameObjects and I need to sort them by their position. My array is

 IEnumerable platforms = GameObject.FindGameObjectsWithTag("Platform").AsEnumerable()  

I have tried using

 platforms = platforms.OrderBy(platform => platform.transform.position);

to sort it, but it has the error

 error CS0411: The type arguments for method `System.Linq.Enumerable.OrderBy<TSource,TKey>(this   

`` System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Is there a better way to do this?

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 ArkaneX · Nov 13, 2013 at 03:49 PM 2
Share

The problem is that you're declaring platforms as non generic IEnumerable. To fix this, you can either use:

 IEnumerable<GameObject> platforms = GameObject.FindGameObjectsWithTag("Platform");
 platforms = platforms.OrderBy(platform => platform.transform.position);

or simply declare it as GameObject[] (you can use var keyword):

 var platforms = GameObject.FindGameObjectsWithTag("Platform");
 platforms = platforms.OrderBy(platform => platform.transform.position).ToArray();

EDIT: converted to comment as this solves only the specific error you described. Actual sort requires changes described in other answers.

avatar image NeverEndingPrjct · Aug 25, 2014 at 06:14 PM 0
Share

2 options...

  1. use LINQ

  2. use an IComparer

1 Reply

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

Answer by Seregon · Nov 13, 2013 at 03:49 PM

You can only sort lists by a single value, whereas a position vector contains 3 values (x, y, z). You need to be more specific about what exactly you want to sort by, as just sorting by position doesn't make any sense, to either the computer or a human.

You could sort by position on the x-axis:

 platforms = platforms.OrderBy(platform => platform.transform.position.x);

or you could sort by distance from the origin:

 platforms = platforms.OrderBy(platform => platform.transform.position.sqrMagnitude);

or you could do something fancier like sorting by location on an x-z grid:

 private float GridRanking(Vector3 Pos)
 {
     return pos.x + 100 * pos.z;
 }
 
 platforms = platforms.OrderBy(platform => GridRanking(platform.transform.position));

The above code is an example only, and should sort items into cells of 1x1 unit on the horizontal plane, so long as no item has an x-coord above 100 (increase the multiplier if it has). It also won't work for negative coordinates.

If you provide more details about what it is your trying to achieve, I can try and produce some better code.

Comment
Add comment · Show 3 · 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 cmpgk1024 · Nov 13, 2013 at 04:06 PM 0
Share

Sorry for not being clear with what I wanted to do - I want to sort the array by the relative position to the GameObject that this script is attached to, closest first.

avatar image Seregon · Nov 13, 2013 at 04:15 PM 1
Share

Ah, in that case the code would look something like this:

 platforms = platforms.OrderBy(platform => (platform.transform.position - transform.position).sqr$$anonymous$$agnitude);

Note that ArkaneX's comment about using IEnumerable may also be correct, and I've not had a chance to test this code.

avatar image Pundek · Aug 25, 2014 at 04:26 PM -1
Share

i think it can will solve your problem:

 platforms= platforms.OrderBy(obj => (obj.transform.position this.transform.position).sqr$$anonymous$$agnitude).ToArray();
 

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

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

C# copy Sorting data of An Array to Another Array 1 Answer

Instantiate a prefab from jsonarray? 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