title: How to define a Comparator in Java that compares based on two variables
body: <p>I want to create a comparator to operate such that a process with a lower arrival time will appear first in a sorting, and if two processes have the same arrival time, the one with the lower process id comes first in the sorting. I tried the following code, but it doesn't seem to be working. Does anyone see a flaw in it?</p>
  
  <pre><code>public class FCFSComparator implements Comparator&lt;Process&gt;
  {
      public int compare(Process o1, Process o2)
      {
          int result = o1.getArrivalTime() - o2.getArrivalTime();
  
          if(result == 0)
          {
              return (o1.getPid() &lt; o2.getPid()) ? -1 : 1;
          }
          else
          {
              return result;
          }
  //        return (result != 0 ? result : o1.getPid() - o2.getPid());
      }
  }
  </code></pre>
  
  <p>To be specific, given the processes as follows</p>
  
  <pre><code>pid = 0 arrival time = 10
  pid = 1 arrival time = 30
  pid = 2 arrival time = 15
  pid = 3 arrival time = 15
  pid = 4 arrival time = 66
  </code></pre>
  
  <p>I get the following ordering at the end</p>
  
  <pre><code>Pid = 0 arrival time = 10
  Pid = 2 arrival time = 15
  Pid = 1 arrival time = 30
  Pid = 4 arrival time = 66
  Pid = 3 arrival time = 15
  </code></pre>