unity sort by distance code example
Example: unity sort by distance
//using System.Linq;
hits = hits.OrderBy(
x => Vector2.Distance(this.transform.position,x.transform.position)
).ToList();
// more efficient
trees = trees.OrderBy((d) => (d.position - transform.position).sqrMagnitude).ToArray();
//Linqless
hits.Sort(delegate(Enemy a, Enemy b)
{return Vector2.Distance(this.transform.position,a.transform.position)
.CompareTo(
Vector2.Distance(this.transform.position,b.transform.position) );
});