Skip to content
Advertisement

Sort A List of an Object with other object’s method

In the bellow code, with such classes, how can I sort an ArrayList of Students by the score the got from Courses List? I mean how can I sort a List of a specific class by an attribute of this class which is a child of another class.

public class Student{
    public ArrayList<Student> students  = new ArrayList<Student>();
    public ArrayList<Course> studentCourses  = new ArrayList<Course>();
    //...
class Course{
    double score;
    
    public double getScore(){
        return this.score;
    }
    //...
}

Advertisement

Answer

Your “Student” class is confusing. Based on the name, it sounds as though this class should represent a single student. But then you have within it a List<Student> which means that every Student object will contain pointers to zero or more other Student objects. What does this relationship represent? Why does a student refer to numerous other students?

Your “Course” class is also confusing, as it contains only a score. If I was defining a university course then I’d imagine it would represent a course at the school/college/university for a particular year, and it would need to contain the following information to be complete:

  • name of the course
  • year in which the course starts
  • the set of students who have registered to take this course
  • the tests/exams/assessments involved in the course
  • the overall score or grade achieved by each student in the course

If this is a homework exercise and you’re keeping it simple, then you could probably ignore the individual tests/exams/assessments and also the start year. But you would still need to decide on a way to map the overall score to each student.

Assuming you create a “Student” class which represents a single student, (and rename your student database class to “Students”) then you could simply create a Map<Student, Score> inside your “Course” class. (You can use Map<Student, Integer> if your scores are simply numeric values.) But note that if you use the Student objects as Map keys then you will need to override the equals and hashcode methods within your “Student” class so that they work correctly. The best way to distinguish between people (who may have the same names, birthdates, etc) is to assign each one a serial number when they register. So your “Student” class could simply contain a long field with ID serial number, and then your equals and hashcode methods would simply check and use this one unique value.

Alternatively, you might prefer to add a map within the “Student” class, Map<Course, Score> so that each student keeps track of their course scores. What you choose will depend on how your application will most frequently be accessing scores (getting course scores per student, or student scores per course, or both).

But the overall message is that each class you create needs to represent one thing, and you need to decide precisely what. Then you need to decide which fields each class needs in order to fully represent that thing.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement