I am trying to compare multiple properties from the object but my code is only capable of comparing the degree property. When debugging in Visual Studio it appears that my code is completely missing the else statement. I would appreciate any tips.
class Student : IComparable
{
private string fName;
private string lName;
private string deg;
private int gra;
public Student(string firstName, string lastName, string degree, int grade)
{
fName = firstName;
lName = lastName;
deg = degree;
gra = grade;
}
public override string ToString()
{
string var = lName + ", " + fName + " (" + deg + ") Grade: " + gra;
return var;
}
public int CompareTo(object obj)
{
Student newStudent = obj as Student;
if (this.deg.CompareTo(newStudent.deg) == 1)
{
return 1;
}
else if (this.deg.CompareTo(newStudent.deg) != 1)
{
return -1;
}
else //this is what my code is ignoring and not ordering by firstname as well
{
if (this.fName == newStudent.fName)
{
return 0;
}
else if (this.fName != newStudent.fName)
{
return -1;
}
else
{
return 0;
}
}
}
}