- Home /
Vector3 resultant array sorting
I have a Vector3 array of positions of multiple objects captured within a sphere cast , is there an easy and neat way I could sort the resultant array ("objectPositions") according to their Y axis values?
Answer by Saad_Khan · Jan 24, 2017 at 04:07 PM
There are many ways to go about this, the easiest would be to use System.Linq , as follows :
using System.Linq;
objectPositions = objectPositions.OrderBy(v => v.y).ToArray<Vector3>();
This will sort the array "objectPositions" according to its Y components;
Never use Linq frequently (in an Update function for example). It takes long time to execute, produces garbage and is not well supported on every platforms.
Answer by Hellium · Jan 24, 2017 at 03:47 PM
Simple search on Google :
http://www.csharp-examples.net/sort-array/
Array.Sort(array, delegate(Vector3 v1, Vector3 v2) {
if( v2.y > v1.y )
return 1 ;
else if( v2.y < v1.y )
return -1 ;
return 0 ;
});
Just looking into using Array.Sort() right now, but the docs for Unity 5 state that it's for JS only and "C# does not use this feature".
What?? Why??
Yes, I know. So why do the Unity docs say C# does not use this feature?
Are there platforms it won't work on?
Are there reasons I shouldn't use it?
Your answer
Follow this Question
Related Questions
MergeSort function acting strange 0 Answers
How to sort get components? 3 Answers
Scoring System ArgumentOutOfRangeException Error 2 Answers
Multiple Cars not working 1 Answer