Hi I am trying to arrange the patients according to their condition that is Serious(S), Routine(R), Critical(C), and Expectant (E). I have the method but it is not implementing correctly as the program runs and builds successfully but it does not arrange them in that order any help would be much appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client 
{

     class Patient: IComparable
     {
          private int patientnumber;
          private string name;
          private string timein;
          private char condition;


          // Default constructor:
          public Patient()
          {
               name = "N/A";
          }

          // Constructor:
          public Patient(string name, int patientnumber, char condition, string timein)
          {
               this.name = name;
               this.patientnumber = patientnumber;
               this.condition = condition;
               this.timein = timein;
          }

          // Printing method:
          public void PrintPatient()
          {
               Console.WriteLine("{0}, {1} {2} {3} ", name, patientnumber, condition, timein);
          }
          #region
          public int CompareTo(object obj)
          {
               return Order(this.condition).CompareTo(Order(((Patient)obj).condition));
          }

          private int Order(char condition)
          {
               int result = 4;
               switch (condition)
               {
                    case 'S': result = 0; break;
                    case 'C': result = 1; break;
                    case 'R': result = 2; break;
                    case 'E': result = 3; break;
               }
               return result;
          }
          #endregion
     }


     class StringTest
     {
          static void Main()
          {
               // Create objects by using the new operator:
               Patient patient1 = new Patient("Daniel Craig",    414092817, 'E', "12:45");
               Patient patient2 = new Patient("Sally May",       654125419, 'S', "03:00");

               // Create an object using the default constructor:
               Patient patient3 = new Patient("Jomo Gold",       124891017, 'C', "11:20");
               Patient patient4 = new Patient("Carlitos Way",    215210211, 'E', "12:20");
               Patient patient5 = new Patient("Keith Baird",     215961011, 'C', "19:50");
               Patient patient6=  new Patient("Sony Webuye",     29154631,  'R',  "12:20");
               Patient patient7 = new Patient("Amy Whitehouse",  215961016, 'C', "21:00");
               Patient patient8 = new Patient("Santos Abreza",   813694011, 'R', "10:20");



               // Display results:
               Console.Write("Patient #1: ");
               patient1.PrintPatient();
               Console.Write("Patient #2: ");
               patient2.PrintPatient();
               Console.Write("Patient #3: ");
               patient3.PrintPatient();
               Console.Write("Patient #4: ");
               patient4.PrintPatient();
               Console.Write("Patient #5: ");
               patient5.PrintPatient();
               Console.Write("Patient #6: ");
               patient6.PrintPatient();
               Console.Write("Patient #7: ");
               patient7.PrintPatient();
               Console.Write("Patient #8: ");
               patient8.PrintPatient();


          }
     }
}

Recommended Answers

All 10 Replies

And where is your code that sorts them?

I'd use an enum for the patient. much easier, to get the char value use the toString() method of the enum.
Then linq becomes your friend:

List<Product> patients = GetProductList();

var sortedPatients =(
from p in patients
orderby p.patient
select p).toList();

But also the post above is correct you are not sorting, although your objects are implementing icomparable you need to call the collection's sort method.

sorry about the Product object I canabilized some other code, that object should of course be patient.

I am supposed to implement a heap based priority Que based on those patients conditions so that it can arrange them in order and I have no Idea where to start this is one of those lectures I did not understand properly.

If you go back to where you originally asked this question you'll find the code to sort this in a heap based manor.

to properly compare two objects, you need to implement the compare method.

example:

private class Patient: IComparer
{
//other class code goes here

//this is the compare method you need
   public int Compare(object a, object b)
   {
      Patient c1=(Patient)a;
      Patient c2=(Patient)b;
      if (Order(c1.patient)> Order(c2.patient))
         return 1;
      if (Order(c1.patient)< Order(c2.patient))
         return -1;
      else
         return 0;
   }
}

//create your array or list of patients by just adding them all to an array or list
//just call the sort method of the array.

Patient p = new Patient();
Patient p2 = new Patient();
Patient p3 = new Patient();
List<patient> l = new List<patient>();
l.add(p);
l.add(p1);
l.Add(p2);
//call the collection's sort method
l.Sort();

Now your list is sorted. Have a nice day ^ ^

This is what I have but its not arranging the patients in the order we want in this priority C,S,E,R

here is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client 
{

     class Patient: IComparable
     {
          private int patientnumber;
          private string name;
          private string timein;
          private char condition;
          #region


          public int CompareTo(object obj)
          {
               return Order(this.condition).CompareTo(Order(((Patient)obj).condition));
          }

          private int Order(char condition)
          {
               int result = 4;
               switch (condition)
               {
                    case 'C': result = 0; break;
                    case 'S': result = 1; break;
                    case 'R': result = 2; break;
                    case 'E': result = 3; break;
               }
               return result;
          }
          #endregion


          // Default constructor:
          public Patient()
          {
               name = "N/A";
          }

          // Constructor:
          public Patient(string name, int patientnumber, char condition, string timein)
          {
               this.name = name;
               this.patientnumber = patientnumber;
               this.condition = condition;
               this.timein = timein;
          }

          // Printing method:
          public void PrintPatient()
          {
               Console.WriteLine("{0}, {1} {2} {3} ", name, patientnumber, condition, timein);
          }
     
     }


     class StringTest
     {
          static void Main()
          {
               // Create objects by using the new operator:
               Patient patient1 = new Patient("Daniel Craig",    414092817, 'E', "12:45");
               Patient patient2 = new Patient("Sally May",       654125419, 'S', "03:00");

               // Create an object using the default constructor:
               Patient patient3 = new Patient("Jomo Gold",       124891017, 'S', "11:20");
               Patient patient4 = new Patient("Carlitos Way",    215210211, 'C', "12:20");
               Patient patient5 = new Patient("Keith Baird",     215961011, 'E', "19:50");
               Patient patient6=  new Patient("Sony Webuye",     29154631,  'C',  "12:20");
               Patient patient7 = new Patient("Amy Whitehouse",  215961016, 'R', "21:00");
               Patient patient8 = new Patient("Santos Abreza",   813694011, 'E', "10:20");



               // Display results:
               Console.Write("Patient #1: ");
               patient1.PrintPatient();
               Console.Write("Patient #2: ");
               patient2.PrintPatient();
               Console.Write("Patient #3: ");
               patient3.PrintPatient();
               Console.Write("Patient #4: ");
               patient4.PrintPatient();
               Console.Write("Patient #5: ");
               patient5.PrintPatient();
               Console.Write("Patient #6: ");
               patient6.PrintPatient();
               Console.Write("Patient #7: ");
               patient7.PrintPatient();
               Console.Write("Patient #8: ");
               patient8.PrintPatient();


          }
     }
}

thanks for the help so far i really appreciate it.

Again, which line do you actually sort the object? Implementing IComparable allows the objects to be sorted using standard sorting methods, but it doesn't automagically sort them for you.

I am supposed to implement a heap based priority Que

Rereading back through this you don't really need a sort, you need a Priority Queue that uses a heap structure as the backing storage.

You should read about Heapsort, paying attention to the method that is usually called 'heapify'.

I was going to do a priority queue class as a snippet but got distracted by skip-lists and then Daniwebs claim that they own everything I post. So no class for you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.