Reputation: 3
I have a text file with some data, numbers and score, and i need to sort this data by high score( 10,9,8,7,6...)
How can I do it?
Maybe by read and create ArrayList? or split it
I don't any idea how to do it
I need to sort this text by Score (2,2,1,0.8,...)
saveButton = new JButton(" Save ");
saveButton.setBackground(Color.white);
saveButton.setForeground(Color.black);
saveButton.setFont(normalFont);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PrintWriter writer = null;
String newLine = System.getProperty("line.separator");
String textFieldVal = textField.getText();
double Dsise = size;
double Dsec = sec;
double pt = (Dsise * Dsise) / Dsec;
DecimalFormat df = new DecimalFormat("#.####");
try {
writer = new PrintWriter(new BufferedWriter(new FileWriter("HighScores.txt", true)));
writer.println(textFieldVal + " (Time: " + sec + ", grid: " + size + "x" + size + ") " + "Score : " + df.format(pt));
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
try {
Check();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
public void Check() throws IOException {
// here i need to sort it
}
}
here i have a text, what i need to sort
I tried do it by Scanner but it doesnt work
Upvotes: 0
Views: 174
Reputation: 22128
It is not clear what you need the output to be but you have essentially the following steps:
Read the file line by line. You can use a BufferedReader
for this, or the more modern Java 8 approach, by getting a Stream
of Strings
, each string represents a line.
Stream<String> lines = Files.lines(Paths.get(fileName));
Translate each String to the information you want to keep. Do you want the name? Do you want the score? Do you want all the information (time, grid etc.). If you just need the score you could find the index of the string Score :
, add 8 to it, and take the substring of the string from there, which will be your score, which you can convert to an Integer. Once you have the data, you just need to sort it using one of the sorting mechanisms provided by collections or streams.
List<Double> scores = lines
.map(line -> line.substring(line.indexOf("Score :") + 8))
.map(Double::parseDouble)
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
If you need the names and the rest of the details, you have to create a specific object that carries the data (name, time etc.), and instead of extracting just the score you populate the object. Sorting would then happen on the score property. From your question it is not clear if this is what you need.
UPDATE: From the comments it seems that all the data is actually required.
To keep all the data you have to first create an object that represents one row in your file. Unfortunately your string is not really consistently formatted, it would have been easier if the values were delimited like:
Bogdan,2,2x2,2
Leo,6,2x2,0.6667
So if you can modify that function you pasted in your question to output this in this format it would be much easier for you, because you just use String.split(",")
. Otherwise you have to find the places where each data item is, with indexOf()
and substring()
and extract it. That is an exercise for you.
public class Player {
private final String name;
private final int time;
private final String grid;
private final double score;
public Player(String name, int time, String grid, double score) {
this.name = name;
this.time = time;
this.grid = grid;
this.score = score;
}
public double getScore() {
return score;
}
// todo: put all the getters
public static Player parse(String line) {
//todo: extract the data items from the string...
}
@Override
public String toString() {
return String.format("%s (Time: %d Grid: %s) Score: %.4f", name, time, grid, score);
}
}
Your stream processing then becomes a bit simpler:
List<Player> scores = lines
.map(Player::parse)
.sorted(Comparator.comparingDouble(Player::getScore).reversed())
.collect(Collectors.toList());
Upvotes: 1