Sorting arrays?

Hey, heres my script;

function OnGUI ()
{
    var arr = new Array();
    arr.length = 5;
    arr[0] = PlayerPrefs.GetString("Score1");
    arr[1] = PlayerPrefs.GetString("Score2");
    arr[2] = PlayerPrefs.GetString("Score3");
    arr[3] = PlayerPrefs.GetString("Score4");
    arr[4] = PlayerPrefs.GetString("Score5");
    arr[5] = PlayerPrefs.GetString("CurrentScore");
    arr.Sort();
    GUI.Label (Rect (10,30,150,100), arr[0]);
    GUI.Label (Rect (10,50,150,100), arr[1]);
    GUI.Label (Rect (10,70,150,100), arr[2]);
    GUI.Label (Rect (10,90,150,100), arr[3]);
    GUI.Label (Rect (10,110,150,100), arr[4]);
}

it should get those values and add the current score, then show the top 5 (add the current score to the board if it is high enough), one problem, it doesn't work. :(

Just a few hints:

  • you set the array length to 5 but you try to set the 6th element (index 5).
  • the highest index is always the element count minus 1. Length 5 means there's space for 5 elements but you start counting at 0 so you end at 4.
  • You used GetString (probably SetString when you saved them) to get the scores. But strings are not numbers and therefore they aren't compared the same way.
  • Like Eric said the Array class is quite bad. It's slow due to dynamically typed elements and can cause problems if you don't cast them to the right type.

"for" statements are quite easy, here's a small explanation:

for (part1; part2; part3)
{
    //loop body
}

All the code within the brackets belong to the loop. If you declare a variable in part 1 it's not available outside of the loop

  1. part1 is executed right before the loop starts. It's used to initialize the loop.
  2. part2 is executed right before each iteration step. This part evaluates to a boolean. If the expression in part 2 returns false the loop is aborted. As long as this evaluates to true the body is executed and the loop runs on.
  3. part3 is executed after the loop body have been executed. This is used to prepare the next iteration step.

A typical loop works with an integer variable that counts up or down:

for (i=0; i<5; i++)

This loop will run 5 times (0, 1, 2, 3, 4). After the 5th iteration (i == 4) the 3th part will increase i again to 5 but when part 2 is evaluating the expression (5<5) it aborts here.

You are not restricted to use an integer variable. Here you can iterate through a transform's hierarchy upwards till you reach the top (the overall parent doesn't have a parent)

for (var T : Transform = transform; T != null; T = T.parent)


The second version is the for (each) loop to iterate through an enumerable object.

for(variable in enumerable)

As example: the Transform class is an enumerable type. You can iterate through all childs of this transform:

for (var T : Transform in transform)

within the for body the variable T always holds one element of the enumerable object.


There are some special "commands" that can be used inside a loop:

  • break; break exits the loop at the current position and continue with the code after the loop-body.
  • continue; continue breaks also the current iteration but it doesn't exit the loop. It just go on with the next iteration. So it just skips the rest of the loop body.