我已经用 Boat 对象填充了 ArrayList。该船具有名称,ID 和分数。我需要相互比较分数,整数,并按从低到高的顺序排序。
我已经尝试了很多,但失败了很多,我对要做的事情感到困惑。
public class Boat {
private String name;
private int id;
private int score;
//Constructor
public Boat(String name, int id, int score) {
this.name = name;
this.id = id;
this.score = score;
}
我找到了 Comparator 类,但无法弄清楚如何正确使用它。
基本上,我需要做的是将分数排序到新的 ArrayList 中。将它们从我的private ArrayList<Boat> participants;
列表降到我的private ArrayList<Boat> sortedScoreList;
列表。
这是我第一次在此处发布信息,因此,如果需要添加更多信息,请告诉我。
1 回答
使用默认的
sort
方法按score
升序排序:使用默认的
sort
方法按score
降序排序:使用蒸汽按
score
升序排序:使用蒸汽按
score
降序排序: