Originally Posted by

**trev456**
Thanks all =) I think my best bet would be to do what KONI said. Read from a file into an array, sort it and then sort the initial array the same way than overwrite the previous one! Now to go try to write it all out!

Btw, you don't have to sort that array at all. Since your highscore list inside the file is already sorted, inserting a new score comes down to finding the position for that element. This is turn is the same as inserting a new element in an already ordered set, exactly like in insertion sort.

Suppose you have a highscore of 10, ordered from highest to lowest:

- initialize an array of 11 elements

- put the 10 highscores in the positions [0,9]

- put the new highscore in the position 10

- let i be the position of the new highscore

- compare i-1 with i, if i is greater than i-1, swap i-1 and i (move the highscore 1 element closer to 0)

- repeat the last step as long as i > 0 or you couldn't swap the element.

- finally, write the 10 first elements in the file

in pseudo-code:

Code:

insert(array a, int length, value) {
int i = length - 1;
while (i >= 0 && a[i] > value) {
a[i + 1] = a[i];
i = i - 1;
}
a[i + 1] := value;
}