我有一个自定义 Java 类,其中包含两个变量:username和score。
我正在创建一个 ArrayList,其中包含多个。然后我想根据它们的值按从低到高的顺序对它们进行排序score
高分班
public class Highscore implements ConfigurationSerializable {
String username;
int score;
public Highscore(String username, int score) {
this.username = username;
this.score = score;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public Map<String, Object> serialize() {
Map<String, Object> mappedObject = new LinkedHashMap<String, Object>();
mappedObject.put("username", username);
mappedObject.put("score", score);
return mappedObject;
}
public static Highscore deserialize(Map<String, Object> mappedObject) {
return new Highscore((String) mappedObject.get("username"),
(int) mappedObject.get("score"));
}
}
例如,下面显示了包含多个 的 ArrayList Highscore。我只想查看score基于从低到高的 ,然后将Highscore' 排序到另一个 ArrayList 中。
ArrayList<Highscore> highscores = new ArrayList<>();
highscores.add(new Highscore("user1", 10));
highscores.add(new Highscore("user2", 0));
highscores.add(new Highscore("user3", -15));
highscores.add(new Highscore("user4", 30));
highscores.add(new Highscore("user5", 5));
// Now, sort the highscores based on their 'score'
提前致谢。
扬帆大鱼
犯罪嫌疑人X
湖上湖
相关分类