title: Java Sorting "queue" list based on DateTime and Z Position (part of school project)
body: <p>For a school project i have a list of 50k containers that arrive on a boat. 
  These containers need to be sorted in a list in such a way that the earliest departure DateTimes are at the top and the containers above those above them.
  This list then gets used for a crane that picks them up in order.</p>
  
  <p>I started out with 2 Collection.sort() methods:</p>
  
  <p>1st one to get them in the right X>Y>Z order</p>
  
  <pre><code>Collections.sort(containers, new Comparator&lt;ContainerData&gt;() 
  {
      @Override
      public int compare(ContainerData contData1, ContainerData contData2)
      {
          return positionSort(contData1.getLocation(),contData2.getLocation());
      }
  });
  </code></pre>
  
  <p>Then another one to reorder the dates while keeping the position in mind:</p>
  
  <pre><code>Collections.sort(containers, new Comparator&lt;ContainerData&gt;() 
  {
      @Override
      public int compare(ContainerData contData1, ContainerData contData2)
      {
          int c = contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());
  
          int p = positionSort2(contData1.getLocation(), contData2.getLocation());
  
          if(p != 0)
              c = p;
  
          return c;
      }
  });
  </code></pre>
  
  <p>But i never got this method to work..</p>
  
  <p>What i got working now is rather quick and dirty and takes a long time to process (50seconds for all 50k):</p>
  
  <p>First a sort on DateTime:</p>
  
  <pre><code>Collections.sort(containers, new Comparator&lt;ContainerData&gt;() 
  {
      @Override
      public int compare(ContainerData contData1, ContainerData contData2)
      {
          return contData1.getLeaveDateTimeFrom().compareTo(contData2.getLeaveDateTimeFrom());
      }
  });
  </code></pre>
  
  <p>Then a correction function that bumps top containers up:</p>
  
  <pre><code>containers = stackCorrection(containers);
  
  private static List&lt;ContainerData&gt; stackCorrection(List&lt;ContainerData&gt; sortedContainerList)
  {
      for(int i = 0; i &lt; sortedContainerList.size(); i++)
      {
          ContainerData current = sortedContainerList.get(i);
  
          // 5 = Max Stack (0 index)
          if(current.getLocation().getZ() &lt; 5) 
          {   //Loop through possible containers above current
              for(int j = 5; j &gt; current.getLocation().getZ(); --j) 
              {   //Search for container above
                  for(int k = i + 1; k &lt; sortedContainerList.size(); ++k)
                      if(sortedContainerList.get(k).getLocation().getX() == current.getLocation().getX())
                      {
                          if(sortedContainerList.get(k).getLocation().getY() == current.getLocation().getY())
                          {
                              if(sortedContainerList.get(k).getLocation().getZ() == j)
                              {   //Found -&gt; move container above current
                                  sortedContainerList.add(i, sortedContainerList.remove(k));
                                  k = sortedContainerList.size();
                                  i++;
                              }
                          }
                      }
                  }
              }
          }
  
      return sortedContainerList;
  }
  </code></pre>
  
  <p>I would like to implement this in a better/faster way. So any hints are appreciated. :)</p>