I have a class student - int age , int height and Name;
I have n objects of the student class and I try to sort then first by age , if there is a tie then by height , if there is a tie randomize name .
I have a class
class StudentComparator implements Comparator{
public int compare(Object 1, Object2)
{
// Logic
}
}
I have a main class
class StudentSorter {
// Initialise student objects etc
// Have an array of students: students[]
Array.Sort(students,new StudentComparator() )
// print values
}
The problem I am facing is that Output does not resemble the logic I have in comparator method of the StudentComparator class. Logic is :
if(Student1.age > student2.age)
{
return 1;
}
else if(Student1.age < student2.age)
{
return -1;
}
else
{
if(Student1.height > Student2.height)
return 1;
else if(Student1.height < Student2.height)
return -1;
else
return 0;
}
Input : 15 6 John 16 5 Sam 17 6 Rooney
output: (no matter How I play around with logic or even comment it)
17 6 Rooney
16 5 Sam
15 6 John
What might be the problem ?