I have a class that looks like this
class Student {
public String str;
public int marks;
}
Say 3 objects for
Student S1 (str: sub1, marks:10) , S2 (str: sub2, marks:5) , S3 (str:s3, marks:2)
I want to ensure that for Student objects that this condition holds true if sub1 > sub2 > sub3 then marks1 > marks2 > marks3
Have that class implementing comparable:
Here’s a working demo. In this demo, the natural order of your class is “descending based on the marks attribute”. That is, greater mark will be placed before a lower mark.