0

I am attempting to create some sort of loop that will check for a high score value in an array, add it to the array if it is higher than some of the other values, and move all of the lower values down.

For example the array is called Integer highScoreArray[12, 9, 8, 7] and has 4 values in it. I have another array that keeps the corresponding dates that the high scores were achieved called String highDateArray["12/5/11", "3/4/11", "4,4/12", "6/6/10"] .

How would I go about adding a new value and corresponding date to the arrays? i.e. If the new value was "10" and date was "5/29/12"?

I don't want to use the sort command on highScoreArray[] because then I will lose track of where the corresponding values where in the highDateArray[]. I am hoping to sort the two arrays in two columns of text in two separate linear layouts afterwards, so trying to keep them separate, am I making this more difficult that it needs to be?

for (int i = 0; i < highScoreArray.length; i++) 
{
    if(newHighScore>=highScoreArray[i])
    {
        // DO SOMETHING   
    }
}
CRM
  • 4,569
  • 4
  • 27
  • 33
clayton33
  • 4,176
  • 10
  • 45
  • 64
  • 1
    Have you tried creating some kind of `HighScore` object that has as fields an `int` for scores and a `String` for dates? Doesn't answer your question, but it might make things easier; it would keep the score paired with correct date, and you could have the `HighScore` object implement `Comparable` so that you can sort arrays of them. – dshapiro May 30 '12 at 02:38

2 Answers2

3

how about storing values and corresponding dates using hashmap? and sort them using values?

hope these links helps you

How to sort hashmap

Sorting HashMap by key

How to sort a Map

Community
  • 1
  • 1
Hassy31
  • 2,793
  • 3
  • 21
  • 37
1

You can create an object and implement Comparable to make it easier.

public class HighScore implements Comparable {
    private int score;
    private Date date; // Or you can just use String

    //  Getters/Setters here
    ...

    public int compareTo(HighScore anotherScore) {
        //  Checking here
        //  Return 0 if this and anotherScore is same
        //  Return 1 if this greater than anotherScore
        //  Return -1 if this smaller than anotherScore
    }

}

So in your loop

for (int i = 0; i < highScoreArray.length; i++) {
    HighScore highScore = highScoreArray[i];
    if (newHighScore.compareTo(highScore) > 0) {
        //  Do something
    }
}
Hanon
  • 3,917
  • 2
  • 25
  • 29