0

I am passing an arrayList to a method to sort its data. The data in the arrayList would contain multiple id numbers correlating to different students. the array will also include other information. How do i sort the arrayList by its id #. I believe it will have to do with my created class which is included below more code can be provided as necessary

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 Answers 3

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

Alternatively, you can have Student implement Comparable<Student>, which means including a .compareTo().

NB: If you're going to override .compareTo(), you should override .equals(), which means you should override hashCode().

Sign up to request clarification or add additional context in comments.

Comments

0

Define a Comparator<Student> and use it with Collections.sort:

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

...then...

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

See the JavaDoc on Comparator for more information.

Comments

0

Create a Comparator for Student, then use Collections.sort()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.