-2

I need your help again. how do I sort the records in the txt file in Java?

Here's the code how i save the scores

try {
    File highscore = new File("highscore.txt");
    PrintWriter output = new PrintWriter(new FileWriter(highscore, true));

    if (highscore.exists()) {
        System.out.println();
        System.out.println("High Score:");
    }

    output.println(name + " - " + totalScore);
    output.close();
} catch (IOException e) {
    System.out.println(e);
}

and here's the code how I display the scores

try {
    FileReader fr = new FileReader("highscore.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;

    while ((s = br.readLine()) != null) {
        System.out.println(s);
    }

    br.close();
} catch (IOException e) {
    System.out.println(e);
}

My current output is:

Player1 100
Player2 200
Player3 50

And i want to sort the score from highest to lowest, how do I go about that? thank you in advance!

the output that I want to get is:

Player2 200
Player1 100
Player3 50
deHaar
  • 17,687
  • 10
  • 38
  • 51
kpAtCh
  • 97
  • 1
  • 8
  • create a `Highscore` class, split `s` on the whitespace and pass the parts into a new instance of `Highscore`, put the `Highscore` instance into a `List` and [sort that list](https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist) – luk2302 Feb 11 '19 at 07:53
  • 6
    I'd really encourage you to give this a try on your own. It sounds like you may need a bit of practice in terms of learning how to break a problem down into its component parts. "I need to print the list sorted, ok, so what do I need for that? The sorted list. Okay, so how do I do that? I sort a list. Okay, so how do I do that? I generate a list. Okay, so how do I do that?..." This is a _critical_ part of learning to program, and asking for the answer on Stack Overflow too early in that cycle robs you of the chance to learn it. – yshavit Feb 11 '19 at 07:54

2 Answers2

0

As per @luk2302 and @yshavit, you'll need to change the reading loop into something else:

while ((s = br.readLine()) != null) {
    // 1. Create a custom object from found lines and push them into a list
}
// 2. Sort the list
// 3. Print the list

You might want to change the saving routine too but that wasn't asked so I'm skipping it.

JussiV
  • 178
  • 3
  • 15
0

i would recommend to use the java sorting function, in this case I would create an object Highscore.class which contains name and score.

public class Highscore {
    private String name;
    private Integer score;

    public Highscore(String name, Integer score) {
        this.name = name;
        this.score = score;
    }

    // getters...
 }

Having that object, you have to create List<Highscore> and sort over that one...

List<Highscore> highscores = new ArrayList();
//add all highscores e.g. highscores.add(new Highscore(name, totalScore));

highscores.sort(Comparator.comparing(Highscore::getScore));

After sorting you can put the highscores into a file.

lhaidacher
  • 161
  • 4