Sorting Collider Arrays by location of elements

Hello everyone,

I am trying to sort an Array of Colliders 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*.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 hitColliders = Physics.OverlapSphere(l_handBase.transform.localPosition, 2.0f, 9);

hitColliders.OrderBy (x => Vector3.Distance (this.transform.position,x.transform.position)).ToArray ();