通过整数内容</class>对arrayList <class>进行排序

时间:2013-11-18 22:58:14

标签: java class sorting arraylist

我将一个arrayList传递给一个方法来对其数据进行排序。 arrayList中的数据将包含与不同学生相关的多个ID号。 该阵列还将包含其他信息。我如何通过其id#对arrayList进行排序。我相信这将与我创建的课程有关,包括在下面 可以根据需要提供更多代码

public static class Student {

    public String firstName;    
    public String lastName;
    public Integer uid;     
    public StudentType type;

    public Student(Student orig) {
        this.firstName = orig.firstName;
        this.lastName = orig.lastName;
        this.uid = orig.uid;
        this.type = orig.type;
    }

    // construct a new student with given fields
    public Student(String firstName, String lastName, Integer newUid, StudentType type) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.uid = newUid;
        this.type = type;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getLastName() {
        return lastName;
    }
    //set type

    public void setType(StudentType type) {
        this.type = type;
    }
    //return type

    public StudentType getType() {
        return type;
    }
    public void setUid(Integer uid){
        this.uid = uid;
    }
     public Integer getUid() {
        return uid;
    }

    // return a string representation of the invoking object
    public String toString() {
        return firstName + " " + lastName + " " + uid + " " + type;
    }

    public static class Graduate extends Student {

        public boolean thesis;
        public ClassStanding study;
        public String profName;

        public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
            super(orig);
            thesis = isThesis;
            this.study = study;
            this.profName = profName;
        }

        public boolean getThesis() {
            return thesis;
        }

        public void setThesis(Boolean thesis) {
            this.thesis = thesis;
        }

        public ClassStanding getStudy() {
            return study;
        }

        public void setStudy(ClassStanding study) {
            this.study = study;
        }

        public String getProfName() {
            return profName;
        }

        public void setProfName(String profName) {
            this.profName = profName;
        }

        public String toString() {
            return super.toString() + thesis + " " + study + " " + profName;
        }
    }

    public static class UnderGraduate extends Student {

        public Major major;
        public Double overallGpa;
        public Double majorGpa;
        public ClassStanding study;

        public UnderGraduate(Student orig, Major major, Double overallGpa, Double majorGpa, ClassStanding study) {
            super(orig);
            this.study = study;
            this.major = major;
            this.overallGpa = overallGpa;
            this.majorGpa = majorGpa;
        }

        public void setMajor(Major major) {
            this.major = major;
        }
        //return type

        public Major getmMajor() {
            return major;
        }

        public void setOverallGPA(Double overallGpa) {
            this.overallGpa = overallGpa;
        }

        public Double getOverallGPA() {
            return overallGpa;
        }

        public void setMajorGPA(Double majorGpa) {
            this.majorGpa = majorGpa;
        }

        public Double getMajorGPA() {
            return majorGpa;
        }

        public ClassStanding getStudy() {
            return study;
        }

        public void setStudy(ClassStanding study) {
            this.study = study;
        }

        public String toString() {
            return study + " " + major + " " + overallGpa + " " + majorGpa;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

Collections.sort(myListofStudents, new Comparator<Student>() {
    @Override
    public int compareTo(Student s1, Student s2) {
          return s1.getUid().compareTo(s2.getUid());
    }
});

或者,您可以Student实施Comparable<Student>,即包含.compareTo()

注意:如果您要覆盖.compareTo(),则应覆盖.equals(),这意味着您应该覆盖hashCode()

答案 1 :(得分:0)

定义Comparator<Student>并将其与Collections.sort一起使用:

public class StudentComparator implements Comparator<Student> {
    @Override
    public int compare (Student s1, Student s2) {
        // Your comparison logic here
    }
}

...则...

Collections.sort(data, new StudentComparator());

有关详细信息,请参阅JavaDoc on Comparator

答案 2 :(得分:0)

Comparator创建Student,然后使用Collections.sort()