Sort a string array according to a float array??

Is it posible to sort a number array and then use the order with a string array?

my case:

I have n dynamic numbers, each one represent a different player.
I constatly sort the array to get the numbers in descending order.
Now… i’d like to print wich player is the one holding it respective number.

Something like this

static var StringedPositions : Array;
var test : Array;


function Update () {




if (testing.playersSizeSelected == 2){

test = new Array (Player1LP.TotalCompletedDistance, Player2LP.TotalCompletedDistance);

test.Sort ();

StringedPositions = new Array ("Player 1" , "Player 2");

StringedPositions.Sort (test);

}



}

Well this code doesn’t work but i don’t know how to do it.

hope you can help me.

LW.

This is the only thing preventing me from finishing my game. I really need some help.

I’ll try to explain better.

The positions in the race is calculated from who’s closest to complete the overall distance of the track * number of laps.

Each player has a script that calculates this and makes a public float var “TotalCompletedDistance”

then I use this array :

var test : float[] = [Player1LP.TotalCompletedDistance, Player2LP.TotalCompletedDistance];

When i sort the array it correctly places the lowes number in the 1st slot and so on…

The problem comes because i need to show in a GUI object wich player is the one with the lowest value inside that array.

For that im guessing that i need to somehow assign a String to each value in the Array. right?

I’ been searching and trying to understand but it proved to be dificult to me.

This is the code I try but still no luck.

static var StringedPlayers : String [];


function Update () {

var test : float[] = [Player1LP.TotalCompletedDistance, Player2LP.TotalCompletedDistance];

StringedPlayers = ["Player 1", "Player 2"];

System.Array.Sort (StringedPlayers, test);


}

You might want to look into using a hash table array, so you can store keys to your values.

hmm ok i’ll look it up althou i don’t know what is it.

You can use one of the many overloads in System.Array.Sort such as this one:

var numbers = [2, 3, 1];
var names = ["beta", "charlie", "alpha"];

System.Array.Sort(numbers, names);

for (i = 0; i < names.Length; i++) {
	print (names[i] + " " + numbers[i]);
}

–Eric

Thx a lot Eric.
It works that way, but i have problems trying to acces the Array “names”.

function OnGUI () {
GUI.Label (Rect (10,50,200,90),  "Pos 1. " + names [0] );
}

And i get null reference exception.

So how can i acces the array? :sweat_smile:

I guess you haven’t initialized the array or put anything in it?

–Eric

Hi eric, thx for keeping it up.

I used exactly the one you proposed before. Justdeclared the static var “names” so it can be accesible from other script.
Maybe i need to do something in the “Start” function?

static var  names : Array;

function Update () {

var numbers = [Player1LP.TotalCompletedDistance, Player2LP.TotalCompletedDistance];
var names = ["Player 1", "Player 2"];

System.Array.Sort(numbers, names);

for (i = 0; i < names.Length; i++) {
   print (names[i] + " " + numbers[i]);

}
}

The above code works fine, but as i said before i can’t acces the array.

You’ve declared a local “names” array in Update, which is not the same as the global “names” array. BTW, you can only sort built-in arrays using System.Array.Sort, not Javascript arrays (which are specific to Unity and not part of .NET). So no Array, only String[ ], int[ ], float[ ], etc.

–Eric

Ohh. Great! i overlooked the var names in update function :sweat_smile:
Thx a lot Eric, Everything working now.
I think I’ll have to read more about Arrays and Unity and see if i can learn this right.

It’s my first game ever, and i started to read about programing 3 weeks ago, so still trying my best to learn. But this was way over my head :smile:

Thx again Eric.

LW.