• 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 EBR · Apr 16, 2014 at 07:44 AM · vector3distanceordering

Sort list of Vector3s by 2 distances

Is it possible to sort a List using 2 different distances. This is hard to explain but, for example: A list of Vector3s is ordered in ascending order from the closest to object A but also the furthest from object b. I know my explanation is terrible but if you get it, is this possible and how should I do it?

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 musaranya · Apr 16, 2014 at 08:19 AM 0
Share

You can order a List by multiple criteria using syntax like this:

 List<Vector3> myList = new List<Vector3>();
 myList = myList.OrderBy(v => v.x).OrderByDescending(v => v.y);

I suppose you can use a function within the lambda expression in order to calculate the distances you are talking about, something like this:

 List<Vector3> myList = new List<Vector3>();
 myList = myList.OrderBy(v => DistanceToA(v)).OrderBy(v => DistanceToB(v));

(where DistanceToA and DistanceToB are two functions that accept a Vector3 as a parameter and return the distance from your objects)

By the way I don't know how do you calculate the distance between a vector3 and an object, I think you need to pick a point from the vector so the distance to the object will be the magnitude of the vector from this point to the nearest point of the object, but well I think this is out of the topic of this post.

Another tip, the proposed query may be a bit inefficient, I don't know, maybe is better to store these distances in a class including a Vector3 and then just have a list of objects of this class which you can sort directly by its properties distanceFromA and distanceFromB.

Hope this helps

avatar image EBR · Apr 16, 2014 at 09:08 AM 0
Share

Thank you I will see which method works best :)

1 Reply

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

Answer by Radivarig · Apr 16, 2014 at 09:00 AM

If you don't depend on total precision in distance get the point in the middle of the two and sort by distance to it.

Otherwise, lets say you have two distance points from the oposite side of all of your elements, since they are 3 dimensional you can take 4-5 of some objects next to you and 2 objects that will be distance points, write down their order by distance from the first object, now go from one direction of that order and compare the distances, from the first swap you make the list doesn't obey the first criteria anymore, so you can't get the list to have 100% of both conditions unless you:

  1. have complementary criterias that exclude one another, like sorting by (x and y, z), (x and z, y) or (y and z, x).

  2. have something all elements have the same in both conditions! For example, sorting a list of people by their last name first and then their first name means that you sort by last name, and then you sort people that have the same last name by their name. Similarly you could see if there are objects equally distanced from the first object and sort those that have the same distance by the second distance. Since that is not a very common thing to happen in 3d space, to have perfectly distanced objects from a point (only objects on a sphere or at the very same location) you could make an offset value that would group objects from almost the same distance and inside those groups sort by distance from another objects. So, sort by the first order, then separate the elements that are clustered that fit close to each other by that first distance, then sort those separate groups by another distance this time ignoring the first criteria causing them to almost fit the first and partially fit the second condition.

Radivarig

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 EBR · Apr 16, 2014 at 09:10 AM 1
Share

Thank you so much for this, it helps alot :)

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

22 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

Related Questions

Getting vectors on an object every so many units 0 Answers

Vector3.Project, how to? 2 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Distance between a gameObject and a direction vector. 1 Answer

Enemy Jitters when i want him to stop a certain distance 0 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