この投稿に従って、カスタマイズされたクラスを保持する配列リストをソートする新しいコンパレータを定義しようとしました。私のコードは次のとおりです。
public class mainClass{
public class match {
/*I defined a new class to hold some data points*/
public int x;
public int y;
public int score;
public match(){
/*initialize values of match if needed*/
}
}
public ArrayList<match> matchList=new ArrayList<match>(); //the arraylist that holds data
/*code that fills the list with matches*/
/*the following method is supposed to sort the matchlist. a match with higher score is bigger, and with the same score, a match with bigger x is bigger*/
public void sortMatchlist(){
Collections.sort(this.matchList,new Comparator<match>(){
@Override
public int compare(match o1, match o2) {
if(o1.score>o2.score) return 1;
else if(o1.score==o2.score){
if(o1.x>o2.x) return 1;
else return 0;
}
else return 0;
}
});
}
ただし、メインで sortMatchList() を呼び出したとき、一致リストは変更されていないようです。何が悪いのかわかりません。誰かが私にいくつかの提案をしてもらえますか? 前もって感謝します