Dynamic Turn Order and Display Based On Initiative Value (Javascript)?

I have searched and searched for an answer to this but I’m coming up short.

In my game, multiple players create a token that represents themself, they are assigned names and Initiative values at runtime by the player and those values are stored in playerprefs.

I’m trying to sort these tokens by descending Initiative values to determine turn order and to display player names in order in a GUI list. It seems like any array that can store a String and an Int as a Key/Value pair (like a hash table) can’t be sorted.

I’m probably missing something really obvious here, but all i need to do is make an array of String(name) and Int(Initiative) pairs, sort the array by the Int(Initiative) value and return the Strings(names) in their new, highest to lowest Initiative order.

so i want to turn some sort of array like this:

token1 with an initiative of 4, token2 with an initiative of 7, token3 with an initiative of 1, token4 with an initiative of 10

into this:

token4 , token2, token1, token3

Also, when a new token is introduced, I need it to slot itself into the correct spot in the initiative order. This would act quite a lot like a scoreboard, where, when a tokens initiative move up the list, the others move down to accommodate it’s new position.

Multidimensional array? Hash table? Some sort of custom sorting? Any help with be much appreciated.

Does anyone know how to pull this off using Javascript?

Thanks so much.

I would look at using a LINQ query.

//The tokens variable should be a IQueryable collection like List
//This will produce a list of player name order by the initiative value

var sortedPlayers = (from t in tokens orderby t.Initiative select t.Name).ToList()

//This would be how you would retrieve a token back from the player name

Token myPlayerToken = (from t in tokens where t.Name == selectedPlayerName select t).FirstOrDefault()

Hope this helps or at least points you in a good direction,

I finally got this to work. For anyone that’s interested:

After failing miserably to understand LINQ queries in Unityscript, I found an example of using bubble sorting to sort one array and apply that sort to another ray.

As the tokens were instantiated, I added their name to an array called “names”, their initiatives to an array named “inits” and then ran this function to sort the names and initiatives based of the initiative values.

function bubbleSort(){
			var foundone : boolean;
			var tempobj : String;
			var tempfloat : int;

					foundone = true;
					  foundone = false;
					  for(n = 0; n < inits.length - 1; n++)
					    if(inits[n] > inits[n + 1])
					      tempobj = names[n + 1];
					      tempfloat = inits[n + 1];
					      names[n + 1] = names[n];
					      inits[n + 1] = inits[n];
					      names[n] = tempobj;
					      inits[n] = tempfloat;
					      foundone = true;

The issue is that this sorts them into ascending order, but to list them highest to lowest, my OnGUI uses a for loop to iterate through the array backwards. Sloppy, but it’s working. Thanks for the input Daniel!