This is a case of the code not being formatted very well. The last line 'return 0;' is not inside the if/else blocks.
Because the if and the else are not using {}, they are single line statements.
If the first condition is true 1 is returned.
If the second condition is true -1 is returned.
Otherwise 0 is returned.
Here is what it looks like if it is aligned more clearly.
// Overriding compare()method of Comparator
// for descending order of cgpa
public int compare(Student s1, Student s2) {
if (s1.cgpa < s2.cgpa) return 1;
else if (s1.cgpa > s2.cgpa) return -1;
return 0;
}
or with {} to better show it:
public int compare(Student s1, Student s2) {
if (s1.cgpa < s2.cgpa) {
return 1;
}else if (s1.cgpa > s2.cgpa) {
return -1;
}
return 0; // this is returned when the items are equal
}