0

I'm reading game scores from a text file into an ArrayList. Each item in the ArrayList is a String array with 2 indexes, one stores the player's name and the other the score.

What's the best way from here to sort the list into numerical order by the score, in order to display high scores?

Thanks!

5
  • What do you mean with "best"? There are lots of ways, some simple, some complex, all of them effort compared to just using a built-in sorter like Collections.sort Commented Nov 24, 2014 at 23:49
  • 3
    you could use Collections.sort() and write your custom comparator. Commented Nov 24, 2014 at 23:49
  • @Mike'Pomax'Kamermans I meant simplest I guess. How would I use Collections.sort here? Commented Nov 24, 2014 at 23:53
  • @BatScream has you covered there. Collections.sort is well documented, as is the concept of using it in combination with a Comparator object, simply search for them within the context of java and you'll find lots of tutorials that explain how to use them. Or search Stackoverflow for it, plenty of questions that involve using Collections.sort, too. Commented Nov 24, 2014 at 23:57
  • 2
    Probably you would be better off with a custom data structure - something like PlayerScore - when your data is as firmly structured as this. And have a List of those, instead of a List of Lists. Then make your class implement Comparable. Commented Nov 25, 2014 at 0:01

2 Answers 2

3

It should look something like this, assuming the score is stored in index 1:

Collections.sort(playerList, new Comparator<String[]>(){
   @Override
   public int compare(String[] player1, String[] player2) {
         return Integer.parseInt(player1[1]) - Integer.parseInt(player2[1]);
     }
 }

playerList is the list of your arrays. This method will sort the array list for you using the supplied Comparator object which, as you see, takes two elements from the ArrayList and supplies a method of determining which one is first.

Sign up to request clarification or add additional context in comments.

Comments

3

If you're not forced to use an array to store the score, then I recommend using a dedicated model class for it, that implements the Comparable interface.

public class Score implements Comparable<Score> {
    final String name;
    final int score;

    public Score(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public int compareTo(final Score that) {
        return that.score - this.score;
    }

    @Override
    public String toString() {
        return String.format("Score[name=%s, score=%d]", name, score);
    }
}

The current implementation sorts descending. If you want to sort ascending, then change it to return this.score - that.score;.

You can use that class like this:

public static void main(String[] args) {
    final List<Score> scores = new ArrayList<>();
    scores.add(new Score("Mike", 100));
    scores.add(new Score("Jenny", 250));
    scores.add(new Score("Gary", 75));
    scores.add(new Score("Nicole", 110));

    Collections.sort(scores);

    for (final Score score : scores) {
        System.out.println(score);
    }
}

The output will be:

Score[name=Jenny, score=250]
Score[name=Nicole, score=110]
Score[name=Mike, score=100]
Score[name=Gary, score=75]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.