Sorting a Queue
<p>I have to simulate a process scheduler using SRTN algorithm and im having trouble within a certain part.</p>

<p>I have a queue of a custom class called 'Process' I need to sort it based on a a field called 'last_prediction'. My code works most of the time, but if you look at time:19 of my <a href="http://pastebin.com/uAtA1UXW" rel="nofollow">output</a>, the output in the ready queue is wrong (it should be: 1004(1.5) 1002(2) 1003(2)). </p>

<p>Here is my code:</p>

<pre><code> int count = ReadyQueue.Count;

        // Copy Queue into Vector
        ArrayList temp = new ArrayList();
        for (int i = 0; i &lt; count; i++)
        {
            Process p = (Process)ReadyQueue.Dequeue();
            temp.Add(p);
        }

        // Sort Vector
        for (int i = 0; i &lt; count; i++)
        {
            double min = ((Process)temp[i]).last_prediction;
            for (int j=i+1; j&lt;count; j++)
            {
                if ( ((Process)temp[j]).last_prediction &lt; min )
                {
                    min = ((Process)temp[j]).last_prediction;
                    Process dummy = (Process)temp[j];
                    temp[j] = temp[i];
                    temp[i] = dummy; 
                }
            }
        }

        // Copy Vector back into Queue
        for (int i = 0; i &lt; count; i++)
        {
            Process p = (Process)temp[i];
            ReadyQueue.Enqueue(p);
        }
</code></pre>

<p>EDIT: ok, im trying to use ICompare, similar to what you gave hughdbrown.Now i get a different error:</p>

<pre><code>public class Process
    {
        public int process_id;
        public int arrival_time;
        public int total_time;
        public int avg_burst;
        public int actual_burst;
        public int last_burst; // SRTN
        public double last_prediction; // SRTN
        public int io_delay;
        public int context_switch_delay;

        public class ProcessSort : IComparer
        {
            public int Compare(object x, object y)
            {
                var a = x as Process;
                var b = y as Process;
                double aNum = a.last_prediction;
                double bNum = b.last_prediction;
                return Compare(aNum, bNum);
            }
        }
    }
</code></pre>

<p>this is the error i get now:</p>

<pre><code>Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object. 
</code></pre>
