Java: How to sort custom type ArrayList

前端 未结 4 1820
时光取名叫无心
时光取名叫无心 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:10

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

    0 讨论(0)
  • 2021-01-14 05:11

    try

    Collections.sort(SortList, new Comparator<Position>(){
                public int compare(Position p1, Position p2) {
                    return p1.z- p2.z;
                }
            });
    
    0 讨论(0)
  • 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<fit1)
                return -1;
        else
                return  0;
    }
    
    0 讨论(0)
  • 2021-01-14 05:15
    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());
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题