- Home /
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?
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
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:
have complementary criterias that exclude one another, like sorting by (x and y, z), (x and z, y) or (y and z, x).
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
Your answer

Follow this Question
Related Questions
Move an object to a specific distance 0 Answers
Vector3.Distance not working properly? 3 Answers
Find a position by knowing the directionVector and the distance to this point 1 Answer
Vector3.Distance between camera and raycasthit 2 Answers
Whats the best way to get the Distance between two Vector3? 2 Answers