I have a list of objects, each of which have a position in 3D space. I need to sort this list by distance to an arbitrary point. Currently I'm doing this with:
_attachedEffectors = _attachedEffectors.OrderBy(x =>
Mathf.Pow((x.transform.position.x - position.x), 2) + Mathf.Pow((x.transform.position.y - position.y), 2) + Mathf.Pow((x.transform.position.z - position.z), 2)
).ToList();
However, unfortunately I'm constrained by using Unity's compiler which is horrible with memory allocation and LINQ/delegates. Is there any way to sort a list like this without using LINQ or delegates? Optimally a search that allocates little or no memory as I need to run this thing many times a frame.
Also in future there may be other, arbitrary constraints on the search (e.g. if the distance from this particular object is more than some object-specific maximum distance, ignore it)
EDIT: I don't think I clearly explained my problem. I'm aware of sorting algorithms, however, all these solutions are regarding 2 independantly comparable objects. I'm asking how to sort these objects in regards to an external variable. That is, they need to be sorted by distance to a given point in space that the objects don't know about, but the sorting algorithm does. I know this could be done by the objects knowing about this point but that screams bad design to me.
Basically, I need a Sort() implementation that takes a parameter as well as the objects to be sorted, and uses that parameter in conjunction with the objects to sort the list (as seen in the LINQ implementation - position is parameter to the function this line is in).