Java: How to sort custom type ArrayList

前端 未结 4 1846
时光取名叫无心
时光取名叫无心 2021-01-14 04:57

I have a custom type Position(x,y,z),now I create a ArrayList, i want to sort this array ordered by the value of z, from small to b

4条回答
  •  既然无缘
    2021-01-14 05:12

    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

提交回复
热议问题