ArrayList list=new ArrayList();
Student student=new Student();
student.setAge(30);
student.setName("张三");
student.setAge(20);
student.setName("张三");
student.setAge(40);
student.setName("张三");
list.add(student);请问我在add时 或者读取list的时候怎么按照age去排序?

解决方案 »

  1.   

     Collections.sort(list, new Comparator<Student>()
                    {
                        public int compare(Student d1, Student d2)
                        {
                            return d1.getAge()-d2.getAge();
                        }
                    });
      

  2.   

    LZ结贴率很高,105.00%
    然后顶LS
      

  3.   

    楼上正解,就是实现一个Comparator接口,然后直接调用Collections.sort方法。
      

  4.   

    Collections类中有些静态方法,可以实现对 集合类的排序,安全管理等。
      

  5.   

    Collections.sort(List<?> list);========================Collections 是 java.util.Collections;
    是一个Collection的帮助类 ,里面有 集合的复制、排序等等通用方法,已经封装好的。
      

  6.   

    ArrayList list=new ArrayList();
    Student student=new Student();
    student.setAge(30);
    student.setnum(2);
    student.setName("张三");
    student.setAge(30);
    student.setnum(1);
    student.setName("李四");
    student.setAge(40);
    student.setName("王五");
    student.setnum(3);
    list.add(student);当有2个数需要排序时 怎么处理?