Trying to sort a list of variables. Position in car racing game.

JavaScript please. I would like to sort and display gameobjects names from highest to lowest according to variables in their respective scripts.
Its actually a list of cars sorted by their position in the race so maybe I can sort the variables and display which position I am in according to my rank in the list. I dont know how to create lists or arrays nor do I know how to use compare or sort functions. Please help!!! It is the last thing to create for my game to be complete.

For example:

“GameObject1” has a script called “Script1” with a variable called “Condition1”

“GameObject2” has a script called “Script2” with a variable called “Condition2”

There 8 GameObjects. I would like to sort the gameobjects from highest to lowest and display the list of objects on screen and watch the list change on screen according to the variables. Is this possible?

well first of all i would assume your game has a master script attached to something neutral other than the cars. if not, you will need one at somepoint before you finish.

so lets assume you have a javascript attached to the camera called “masterscript”. and then the cars could report positions or variable to that script for calculation.

so each cars individual script would say at the top above update something like this:

var cam:GameObject;
       cam=gameObject.Find("Main Camera");

var mscript: level =  cam.GetComponent(level);
       mscript =  cam.GetComponent(level);

obviosly each car should have a variable in its script to somehow know what car number it is.
var carnumber:int;

if(transform.name=="object0"){carnumber=0;}
if(transform.name=="object1"){carnumber=1;}
if(transform.name=="object2"){carnumber=2;}

set up an array in the masterscript to recieve car positions. if you know you always have eight car its as simple as this:

var carpositions:float[];
      carpositions=new float[8];

now that you have this set up, each car reports stuff into the array on the master script with this:

var distancefromstartline:float;
mscript.carpositions[carnumber]=distancefromstartline;

now if you’re still following me you should have a happy array of all the car positions neatly packed into one script to display or do whatever.

arrays are really cool and are very handy

there are many ways to do it but here is an example of pageing through the array to find your highest car!

var winningcarnumber:int;
var f:float;
var i:int;

i=carpositions.Length;
f=0;
while(i>0){i=i-1;
if(carpositions_>f){f=carpositions*;*_

winningcarnumber=i;}
}

keep in mind that if you have 8 cars. the first car is zero. your array is 0-7; there is not an 8. its best to name them all accordingly to keep things uniform in your project.
hope this all helps to get you thinking in the right direction. your masterscript could simply display the winning car number on screen or even all positions or whatever. that’s another question!