Java中自定义arraylist的排序

Java中自定义arraylist的排序,java,sorting,Java,Sorting,根据,我尝试定义一个新的比较器来对包含自定义类的arraylist进行排序。我的代码如下: 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(){

根据,我尝试定义一个新的比较器来对包含自定义类的arraylist进行排序。我的代码如下:

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;
        }
    });
}
if(o1.score > o2.score) return 1;
if(o1.score < o2.score) return -1;
// Scores are equal - compare the "x"-s
if(o1.x > o2.x) return 1;
if(o1.x < o2.x) return -1;
return 0;
public类mainClass{
公开课比赛{
/*我定义了一个新类来保存一些数据点*/
公共int x;
公共智力;
公众智力得分;
公开比赛{
/*如果需要,初始化匹配的值*/
}
}
public ArrayList matchList=new ArrayList();//保存数据的ArrayList
/*用匹配项填充列表的代码*/
/*下面的方法用于对匹配列表进行排序。分数越高的匹配越大,分数相同的匹配x越大*/
公共无效sortMatchlist(){
Collections.sort(this.matchList,newcomparator()){
@凌驾
公共整数比较(匹配o1,匹配o2){
如果(o1.分数>o2.分数)返回1;
否则如果(o1.score==o2.score){
如果(o1.x>o2.x)返回1;
否则返回0;
}
否则返回0;
}
});
}

但是,当我在main中调用sortMatchList()时,匹配列表似乎没有改变。我不知道出了什么问题。有人能给我一些建议吗?提前感谢这应该可以完成以下工作:

if (o1.score == o2.score) {
    return o1.x - o2.x;
} else {
    return o1.score - o2.score;
}
compare
的输出不必是1、-1或0。它必须是正的、零的或负的来表示顺序。因此,我认为只返回
x
分数的差值更清晰


另一方面,根据Java中的命名约定,类名应该以大写字母开头,因此您的类
match
应该命名为
match

,这应该可以完成以下工作:

if (o1.score == o2.score) {
    return o1.x - o2.x;
} else {
    return o1.score - o2.score;
}
compare
的输出不必是1、-1或0。它必须是正的、零的或负的来表示顺序。因此,我认为只返回
x
分数的差值更清晰


另一方面,根据Java中的命名约定,类名应以大写字母开头,因此您的类
match
应命名为
match

逻辑如下:

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;
        }
    });
}
if(o1.score > o2.score) return 1;
if(o1.score < o2.score) return -1;
// Scores are equal - compare the "x"-s
if(o1.x > o2.x) return 1;
if(o1.x < o2.x) return -1;
return 0;
if(o1.score>o2.score)返回1;
如果(o1.scoreo2.x)返回1;
如果(o1.x

您当前的代码没有正确地断开连接:当分数相等时,它返回
0
,但
o1.x
,使项目的排序顺序不正确。

逻辑应如下所示:

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;
        }
    });
}
if(o1.score > o2.score) return 1;
if(o1.score < o2.score) return -1;
// Scores are equal - compare the "x"-s
if(o1.x > o2.x) return 1;
if(o1.x < o2.x) return -1;
return 0;
if(o1.score>o2.score)返回1;
如果(o1.scoreo2.x)返回1;
如果(o1.x

您当前的代码没有正确地打破平局:当分数相等时,它返回
0
,但是
o1.x
,使项目的排序顺序不正确。

您的上一个else语句应该返回-1。@Perception为什么需要为-1?0不是已经表示o1大于o2吗?否。0表示它们大于o2是相等的。集合。sort()是稳定的排序,因此相等的元素不会被重新排序。您的上一个else语句应该返回-1。@Perception为什么它需要是-1?0不是已经表示o1大于o2吗?否。0表示它们相等。集合。sort()是稳定的排序,因此相等的元素不会被重新排序。