- Home /
Sorting Javascript array by Distance
Hello I am trying to sort out an Array into order by distance I have looked online and wondered if anyone can help, I have code that does this in C# but really wanted some advise how to do this in Javascript. The script I have so far is
#pragma strict
var go : GameObject[];
function Start ()
{
go =GameObject.FindGameObjectsWithTag("Enemy");
FindDist ();
}
function FindDist ()
{
for(var i : int = 0; i < go.Length; i++)
{
var dist = Vector3.Distance(go[i].transform.position, transform.position);
print(i +" " + dist);
}
}
This adds Enemys to Array and the for loop tells me the distance, just wondered if someone can advise if something like Array.Sort() could be use to get them into order.
Thanks Dean
Your answer
![](https://koobas.hobune.stream/wayback/20220613200358im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using for loop to gather all waypoint objects near player 2 Answers
geting the closest object from a array 2 Answers
I have an array of players. How can I get my enemy to go to the closest one? 0 Answers
Where should I use a traditional forloop as opposed to 'for (var i in monsters)'? 1 Answer
Check distance between many objects 3 Answers