Reputation: 59
So I'm drawing a blank on this error. Failed to compare two elements in the array. The Array.Sort(patient); is where the error is accruing. I do have a IComparable interface, and a class file with the following code: Trying to sort by patient ID number
class Patient : IComparable
{
private int patientID;
private string patientName;
private int patientAge;
private decimal amount;
public int PatientId { get; set; }
public string PatientName { get; set; }
public int PatientAge { get; set; }
public decimal PatientAmount { get; set; }
int IComparable.CompareTo(Object o)
{
int value;
Patient temp = (Patient)o;
if (this.PatientId > temp.PatientId)
value = 1;
else if (this.PatientId < temp.PatientId)
value = -1;
else
value = 0;
return value;
}
}
and this is what's in my main method. Didn't add the Display() cause nothing added to it now, why it's commented out
private static void Main(string[] args)
{
int numOfPatients =2 ;
Patient[] patient = new Patient[numOfPatients];
for (int x = 0; x < numOfPatients; x++)
{
int intvalue;
decimal dollarValue;
patient[x] = new Patient();
Console.Write("Patient {0}: ", (x + 1));
Console.WriteLine("Enter the Patients ID: ");
bool isNum = int.TryParse(Console.ReadLine(), out intvalue);
if (isNum)
{
patient[x].PatientId = intvalue;
}
else
{
Console.WriteLine("Patient ID was invalid. ID needs to be numbers");
Console.WriteLine("Enter the Patients ID: ");
int.TryParse(Console.ReadLine(), out intvalue);
}
Console.WriteLine("Enter the Patients Name: ");
patient[x].PatientName = Console.ReadLine();
Console.WriteLine("Enter the Patients Age: ");
bool isAge = int.TryParse(Console.ReadLine(), out intvalue);
if (isAge)
{
patient[x].PatientAge = intvalue;
}
else
{
Console.WriteLine("Patient Age was invalid. Age needs to be numbers");
Console.WriteLine("Enter the Patients Age: ");
int.TryParse(Console.ReadLine(), out intvalue);
}
Console.WriteLine("Enter the Patients Amount Due: ");
bool isAmount = Decimal.TryParse(Console.ReadLine(), out dollarValue);
if (isAmount)
{
patient[x].PatientAmount = dollarValue;
}
else
{
Console.WriteLine("Patient amount Due was invalid. Amount needs to be a numbers");
Console.WriteLine("Enter the Patients Amount Due: ");
int.TryParse(Console.ReadLine(), out intvalue);
}
}
Array.Sort(patient);
Console.WriteLine("Patients in order with Amounts Owed are: ");
for (int i = 0; i < patient.Length; ++i) ;
//Display(patient[i], numOfPatients);
Upvotes: 2
Views: 387
Reputation: 5395
It looks like if Array.Sort is passed with a typed array would call the Array.Sort<T>(T[])
overload. According to the MSDN documentation, this overload uses the IComparable<T>
interface to compare objects. So it looks like you have two choices:
IComparable<T>
instead of IComparable
(better). Array
to call the Array.Sort(Array)
overload which uses the IComparable
interface (worse).Upvotes: 0
Reputation: 23
I would just write
return this.PatientId.CompareTo(temp.PatientId)
inside the overriden CompareTo method the class. No need to use equality symbols. This will do the int compare for you and return the correct value.
I also suggest that you just use some implementation of the IList class, and then you can use LinQ statements. Using this will prevent there ever being a null value in the "array"
Upvotes: 1
Reputation: 8805
A few things come to mind:
a) Why not implement IComparable<Patient>
?
b) Why re-implement int.CompareTo(int)
? Your implementation of IComparable could just return this.PatientID.CompareTo(other.PatientID)
.
c) Are you sure the array is full when you're sorting it? I'm not sure what would happen if it contains null
.
Upvotes: 2