简体   繁体   中英

How to sort instances of a class based in ascending order based on the average of the values in an array?

I would like to sort 3 object of the student class in ascending order by
the calculated average of the float array in the students class. I created an interface which accepts two parameters and returns the average of the student but I am not sure exactly how to go about sorting 3 object of the student class by the calculated average.

public class Student implements ICalculateAvg
{
    private String name;
    private String gender;
    private String idNumber;
    private Subject subject;

    //Getters and setters

    @Override
    public float calculateAverage(float firstGrade, float secondGrade) {
        return (firstGrade+secondGrade)/subject.getGrade().length;
    }
}

public class Subject 
{
    private String subjectCode[];
    private String  name[];
    private float grade[];

    //  Getters and setters
}

Printing the contents of the student class produces the following output below.

Student [name=Mary, gender=Female, idNumber=105, subject=Subject [subjectCode=[SPN3010, FRN1720], name=[Spanish, French], grade=[78.0, 98.0]]]
Student [name=John, gender=Male, idNumber=102, subject=Subject [subjectCode=[SPN3010, FRN1720], name=[Spanish, French], grade=[90.0, 91.0]]]
Student [name=James, gender=Male, idNumber=101, subject=Subject [subjectCode=[SPN3010, FRN1720], name=[Spanish, French], grade=[12.0, 34.0]]]

This is some of the contents in my main method. I attempted to use the comparator and comparable interface to accomplish the desired result but I was not able to get it to work.

float [] studenGrades1 = {12f, 34f};
float [] studenGrades2 = {90f, 91f};
float [] studenGrades3 = {78f, 98f};

Student student1 = new Student("James Brown", "Male", "101", new Subject(subjectCode, subjectName, studenGrades1));
Student student2 = new Student("John Doe", "Male", "102", new Subject(subjectCode, subjectName, studenGrades2));
Student student3 = new Student("Mary Jame", "Female", "105", new Subject(subjectCode, subjectName, studenGrades3));

float studentFinalGrade1 = student1.calculateAverage(student1.getSubject().getValueAtArrayIndex(0), student1.getSubject().getValueAtArrayIndex(1));
float studentFinalGrade2 = student2.calculateAverage(student2.getSubject().getValueAtArrayIndex(0), student2.getSubject().getValueAtArrayIndex(1));
float studentFinalGrade3 = student3.calculateAverage(student3.getSubject().getValueAtArrayIndex(0), student3.getSubject().getValueAtArrayIndex(1));

I'm not giving whole code but the logic:

            Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                float grade1[] = s1.getSubject().getGrade();
                float grade2[] = s2.getSubject().getGrade();
                Float avg1=null;//calculate average from grade1
            Float avg2=null;//calculate average from grade2

            return avg1.compareTo(avg2);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM