Search code examples
c#icomparable

Using IComparable


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);

Solution

  • 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.