Inserting an element into a sorted list
- by Russell Cargill
Ok I'm using getSharedPreferences to store my high score but before I fill it up I wanted to sort the scores into ascending order via and array, but if it finds a Score less than it in the first pos then it wont check the rest for the smallest?
//function to add score to array and sort it
public void addscoretoarray(int mScore){
for(int pos = 0; pos< score.length; pos++){
if(score[pos] > mScore){
//do nothing
}else {
//Add the score into that position
score[pos] = mScore;
break;
}
}
sortArray(score);
}
should I call sortArray() before and after the loop to fix this problem or is there a better method to achive the same results?
I should also mention that the sortArray(score) funtion is just calling Arrays.sort(score)
where score is an array of mScore