According to this post, I tried defining a new comparator to sort an arraylist holding a customized class. My code is as follows:
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;
}
});
}
When I called the sortMatchList() in main however, the matchlist seems to be unchanged. I can't figure out what's wrong. Can somebody give me some suggestions? Thanks in advance