tags:

views:

60

answers:

2

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 ?

A: 

If the first value is the age, as your sorted by age, the output is normal.

Colin Hebert
My problem is that even when I change the logic the out out remains same. What I mean by logic is suppose I do something like 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; }
Eternal Learner
@Eternal Learner, did your recompile ? Are you sure that you're using the last version of your code ?
Colin Hebert
yeah I did recompile my code - and I get the same output every time I change logic
Eternal Learner
To verify this, put `if (true) return 0;` at the very beginning of the `compareTo` method. This should leave the order as-is. If it doesn't, step through your code with a debugger to find the problem.
Roland Illig
A: 

Your students are ordered correctly. You just got the order direction wrong.

package so3898183;

import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {

  @Override
  public int compare(Student student1, Student student2) {
    if (student1.age < student2.age)
      return -1; // if the first student is "smaller", return something small
    if (student1.age > student2.age)
      return 1; // if the first student is "larger", return something large

    if (student1.height < student2.height)
      return -1;
    if (student1.height > student2.height)
      return 1;

    return 0;
  }

}

Some remarks to the other solutions:

  • Don't use the "trick" of subtracting student1.age - student2.age and examine the sign of the result. This will overflow for large numbers and may produce incorrect results.
  • Keep your code as simple and readable as possible.
  • Don't allocate extra objects (for example the int[] proposed in another response) when you don't have to.
Roland Illig