Inserting an element into a sorted list

Posted by Russell Cargill on Stack Overflow See other posts from Stack Overflow or by Russell Cargill
Published on 2012-12-08T10:25:45Z Indexed on 2012/12/08 11:05 UTC
Read the original article Hit count: 225

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about android