class User{
    private Student student;
    .....
}
class Student{
    private String name;
    private int age;
    private int score;
}假设我有这样一个集合List<User>, 我想对这个集合按照属性Student的age降序,score升序进行排序 要如何实现? 请高手指点不胜感激。

解决方案 »

  1.   

    可以使用Collections的排序和Comparator来进行排序.
    import java.util.*;public class Test {
    public static void main(String[] args) {
    List<Student> st = new ArrayList<Student>();
    st.add(new Student("Hello", 20, 80));
    st.add(new Student("Hi", 25, 80));
    st.add(new Student("John", 22, 90));
    st.add(new Student("Tomas", 24, 70));
    st.add(new Student("Judo", 20, 85));

    Collections.sort(st, new Comparator<Student>() {
    public int compare(Student a, Student b) {
    if (a.age > b.age){
    return -1;
    } else if (a.age < b.age){
    return 1;
    } else {
    return a.score - b.score;
    }
    }
    });

    System.out.println(st);

    }
    }class Student{ 
    public Student(String name, int age, int score) {
    this.name = name;
    this.age = age;
    this.score = score;
    }

    @Override
    public String toString() {
    return "(" + name + "," + age + "," + score + ")";
    }

        public String name; 
        public int age; 
        public int score; 
      

  2.   


    public class Test {    public static void main(String[] args) {
            List<User> st = new ArrayList<User>();
            st.add(new User(new Student("Hello", 20, 80)));
            st.add(new User(new Student("Hi", 25, 80)));
            st.add(new User(new Student("John", 22, 90)));
            st.add(new User(new Student("Tomas", 24, 70)));
            st.add(new User(new Student("Judo", 20, 85)));
            
            Collections.sort(st, new Comparator<User>() {
                public int compare(User a, User b) {
                    // 按照属性Student的age降序
                    int value = b.getStudent().getAge() - a.getStudent().getAge();
                    
                    if (value == 0) {
                        // score升序进行排序
                        value = a.getStudent().getScore() - b.getStudent().getScore();
                    }
                    
                    return value;
                }
            });
            
            for (User user : st) {
                System.out.println(user.getStudent().getName());
            }
        }
    }public class User {
        private Student student;
        public User(Student student) {
            this.student = student;
        }
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        } 
    }public class Student {
        private String name;
        private int age;
        private int score;
        public Student(String name, int age, int score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getScore() {
            return score;
        }
        public void setScore(int score) {
            this.score = score;
        } 
    }
      

  3.   

    也可以使用实现Comparable接口的方法
      

  4.   


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;class Student implements Comparable<Student>
    {
        public String name;    public int age;    public int score;    public Student(String name, int age, int score)
        {
            this.name = name;
            this.age = age;
            this.score = score;
        }    public int compareTo(Student s)
        {
            // age降序,score升序
            if (this.age > s.age)
            {
                return -1;
            }
            else if (this.age < s.age)
            {
                return 1;
            }
            else
            {
                return this.score - s.score;
            }
        }    @Override
        public String toString()
        {
            return name + "    " + age + "    " + score;
        }
    }public class Test
    {
        public static void main(String[] args)
        {
            List<Student> st = new ArrayList<Student>();
            st.add(new Student("Hello", 20, 80));
            st.add(new Student("Hi", 25, 80));
            st.add(new Student("John", 22, 90));
            st.add(new Student("Tomas", 24, 70));
            st.add(new Student("Judo", 20, 85));        Collections.sort(st);        System.out.println("name    age    score");
            for (Student s : st)
            {
                System.out.println(s.toString());
            }
        }
    }