# Sorting Collider Arrays by location of elements

Hello everyone,
I am trying to sort an `Array` of `Collider`s which I get the following way:

``````Collider[] hitColliders = Physics.OverlapSphere(l_handBase.transform.localPosition, 2.0f, 9);
``````

This returns me colliders, from which I then need to sort the `x` position of the parent of the transform of the collider.

Explanation (not actually valid code):

``````for( int i=0 ; i<hitColliders.Length ; i++ )
hitColliders[i].transform.parent.localPosition.x
``````

I need to sort the colliders by the `x` values of the `localPosition`, I don’t think I can just use a `float` `List`, since I wouldn’t be able to access the colliders directly anymore.

### Any help would be greatly appreciated!

Hey, dude… I literally just got this ^^

You must use this on your code:

``````using System.Linq;
``````

then use:

``````Collider[] colliders = Physics.OverlapSphere( overlapSphereCenter , 2.0f , 9 );
Vector3 sortingOrigin = transform.position;
colliders.OrderBy( col => Vector3.Distance(sortingOrigin,col.transform.position) );
``````

Hi, I know this is old, but just in case someone stumbles upon this when searching for a solution like me

At least one object must implement IComparable

I initially thought this was because `Collider` didn’t implement the comparable interface, but in fact it was because ColliderDistance does not (this is the object that the method `.Distance()` returns).

So if you want to order an array or list of colliders, you can use this:

``````using System.Linq;
``````
``````var colliders = Physics.OverlapSphere(center, radius);
var orderedByProximity = colliders.OrderBy(c => (center - c.transform.position).sqrMagnitude).ToArray();
``````

I found this solution by @derHugo on StackOverflow:

Hope it helps anyone, since this was the first post that came up when searching on google