Sort Two Arrays

Hi everyone,

I've started the Hack and Slash tutorial on BurgzergArcade. It is shown in c# but i've been using javascript and I now need to sort an array based on another.

One array is a list of GameObjects and the other is a list of float variables, which are the distance between each gameobject and the character.

What I need to do is sort the distances in ascending order and also apply the same sort to the gameobject.

For example: Both arrays start like this:

Array1 = [Enemy1, Enemy2, Enemy3];
Array2 = [20, 10, 30];

And when sorted should end up like this:

Array1 = [Enemy2, Enemy1, Enemy3];
Array2 = [10, 20, 30];

I'd use a bubble sort, since it's easy to program. Unity has built-in sort, but only a basic one that sorts the items in the array with regard to each other.

PS: This code is untested, so it may not just work out of the box. But I hope it gets across the idea.

var foundone : bool;
var tempobj : GameObject;
var tempfloat : float;

foundone = true;
while(foundone)
{
  foundone = false;
  for(i = 0; i < Array2.length - 1; i++)
  {
    if(Array2 *> Array2[i + 1])*
 *{*
 *tempobj = Array1[i + 1];*
 *tempfloat = Array2[i + 1];*
 _Array1[i + 1] = Array1*;*_
 <em>_Array2[i + 1] = Array2*;*_</em>
 <em><em>_Array1 *= tempobj;*_</em></em>
 <em><em><em>_Array2 *= tempfloat;*_</em></em></em>
 <em><em><em>_*foundone = true;*_</em></em></em>
 <em><em><em>_*}*_</em></em></em>
 <em><em><em>_*}*_</em></em></em>
<em><em><em>_*}*_</em></em></em>
<em><em><em>_*```*_</em></em></em>

@almo
Can I say that I have just spent the last 2 hours laboring over how to get this working. I tried every goddamn bubble sort algorithm I knew to man, but because my knowledge is mostly python it just all screwed up. You absolutely saved my life here man, thank you so much for this. I know this was posted a very long time ago for you, but I still need to thank you for this regardless.