Question: How to I directly compare manually entered student infomation (shown below = 'Elise') with other student Aggergate marks, then sort the data with the largest 'Aggergate' number at the top of the output file.
public class Student2 implements Comparable<Student2> {
@Override
public int compareTo(Student2 o) {
return Double.valueOf(this.aggerate()).compareTo(Double.valueOf(o.aggerate()));
if (result > 0) {
return 1;
} else if (result < 0) {
return -1;
} else {
return 0;
}
}
public static void main(String... args) {
Set<Student2> Stu = new TreeSet<>(
Comparator.comparing(Student2::aggregate).reversed()
.andThen(Student2::getId));
Stu.add(new Student2(25321, "Elsie", 51.5, 45.6, 48.5, "3rd", "Proceed To Stage 2"));
Stu.stream().forEach((c) -> {
System.out.println(c);
});
}
public double aggerate() {
DecimalFormat decFormat = new DecimalFormat("#.#");
double aggerateMarks = Double.valueOf(decFormat.format((IR101Grades + IR102Grades) / 2));
aggerateMarks = Math.round(aggerateMarks * 100.0) / 100.0;
return aggerateMarks;
}
public String toString() {
return "\n" + studentID + " |" + studentName + "\n" + "IR101: " + IR101Grades + " | " + "IR102: " + IR102Grades + " |"
+ " Aggregate Mark: " + aggerate() + "\n" + "Class: " + setGrade() + " | " + "Outcome: " + setClass()
+ "\n" + "\n" + "-----------------------------------------------------" + "\n" + "\n";
}
To create a TreeSet which reverse sorts by aggregate you can do
Set<Student2> Stu = new TreeSet<>(
Comparator.comparing(Student2::aggregate).reversed()
.andThen(Student2::getId));
This way your tree will be in reverse order.
Note: just in case two students have the same aggregate, the getID should ensure they are never considered a duplicate
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.