Java:如何对自定义类型 ArrayList 进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19064109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:48:38  来源:igfitidea点击:

Java: How to sort custom type ArrayList

javacollectionsarraylistquicksortmergesort

提问by atom2ueki

I have a custom type Position(x,y,z),now I create a ArrayList<Position>, i want to sort this array ordered by the value of z, from small to bigger,how can i do that using Collections.sortor is there any other efficient sorting method?

我有一个自定义类型Position(x,y,z),现在我创建了一个ArrayList<Position>,我想按 z 的值排序这个数组,从小到大,我该如何使用Collections.sort或者是否有任何其他有效的排序方法?

When I try to use

当我尝试使用

public class PositionComparator implements Comparator<Position> {

        @Override
        public int compare(Position o1, Position o2) {
            // TODO Auto-generated method stub
            return o1.height().compareTo(o2.height());

        }

    }

get an error

得到一个错误

Cannot invoke compareTo(double) on the primitive type double

回答by Adam Siemion

You need to implement your Comparator, which will compare the value of the zattribute.

您需要实现您的Comparator,它将比较z属性的值。

回答by upog

try

尝试

Collections.sort(SortList, new Comparator<Position>(){
            public int compare(Position p1, Position p2) {
                return p1.z- p2.z;
            }
        });

回答by sunysen

Collections.sort  

for example

例如

class User {

    String name;
    String age;

    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import java.util.Comparator;

public class ComparatorUser implements Comparator {

    public int compare(Object arg0, Object arg1) {
        User user0 = (User) arg0;
        User user1 = (User) arg1;

        int flag = user0.getAge().compareTo(user1.getAge());
        if (flag == 0) {
            return user0.getName().compareTo(user1.getName());
        } else {
            return flag;
        }
    }

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SortTest {

    public static void main(String[] args) {
        List userlist = new ArrayList();
        userlist.add(new User("dd", "4"));
        userlist.add(new User("aa", "1"));
        userlist.add(new User("ee", "5"));
        userlist.add(new User("bb", "2"));
        userlist.add(new User("ff", "5"));
        userlist.add(new User("cc", "3"));
        userlist.add(new User("gg", "6"));

        ComparatorUser comparator = new ComparatorUser();
        Collections.sort(userlist, comparator);

        for (int i = 0; i < userlist.size(); i++) {
            User user_temp = (User) userlist.get(i);
            System.out.println(user_temp.getAge() + "," + user_temp.getName());
        }

    }
}

回答by alex

I use this (just an example I cut and past but same idea) for descending sort:

我使用这个(只是我删减和过去但同样的想法的一个例子)进行降序排序:

@Override
public int compare(Member m1, Member m2) {

    double fit1 = m1.getFitness() ;
    double fit2 = m2.getFitness() ;
    if (fit2>fit1)
            return 1;
    else if (fit2<fit1)
            return -1;
    else
            return  0;
}