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?

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;

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 ;
              });