title: Java Object Comparable using many attributes
body: <p>I have some orders to be loaded and unloaded in delivery routes, one route can have one <code>OrderDocument</code> asigned to it to be loaded, unloaded or both.
  Each order-document is ordered by an index in the route assigned. So the class looks like this:</p>
  
  <pre><code>class OrderDocument implements Comparable&lt;ModelTask&gt;{
      int order_id;  // The document ID
      int route_load_id;  // The route this order is assigned to be loaded
      int route_unload_id;  // The route this order is assigned to be loaded, may be same as route_load_id and viceversa
      int route_load_index;  // The index order in load route
      int route_unload_index;  // The index order in unload route
  }
  </code></pre>
  
  <p>Then I have the Route class:</p>
  
  <pre><code>class Route{
      int id; // ID of route, this value should be in OrderDocument.route_load/unload_id
      ArrayList&lt;OrderDocument&gt; orders;  // **I WANT TO SORT THIS LIST**
     ...
  </code></pre>
  
  <p>So, I have a function that create a Route instance and adds OrderDocument objects to its <code>orders</code> ArrayList so the List should have Documents that <code>Route.id</code> is in <code>route_load_id</code> or in <code>route_unload_id</code></p>
  
  <p><strong>The main problem</strong></p>
  
  <p>I need this list to be sorted by load/unload index if the route id is in load_id or unload_id.
  Actually I have the following <code>Comparator</code>  aproach but is not working.</p>
  
  <pre><code>private class OrdersComparable implements Comparator&lt;OrderDocument&gt;{
      @Override
      public int compare(OrderDocument x, OrderDocument y) {
          if (x.route_load_id == y.route_load_id)
              return x.route_load_index - y.route_load_index;
          else if (x.route_load_id == y.route_unload_id)
              return x.route_load_index - y.route_unload_index;
          else if (x.route_unload_id == y.route_load_id)
              return x.route_unload_index - y.route_load_index;
          else if (x.route_unload_id == y.route_unload_id)
              return x.route_unload_index - y.route_unload_index;
          else
              return 0;
      }
  }
  </code></pre>
  
  <p><strong>Example</strong></p>
  
  <p>4 Orders in the Route-43, 2 to be loaded, 4 to be unloaded.</p>
  
  <pre><code>OrderDocument('order_id':1, 'route_load_id':43, 'route_unload_id':null, 'route_load_index':0, 'route_unload_index':null)
  OrderDocument('order_id':2, 'route_load_id':43, 'route_unload_id':null, 'route_load_index':2, 'route_unload_index':null)
  OrderDocument('order_id':3, 'route_load_id':null, 'route_unload_id':43, 'route_load_index':null, 'route_unload_index':1)
  OrderDocument('order_id':1, 'route_load_id':null, 'route_unload_id':43, 'route_load_index':null, 'route_unload_index':3)
  </code></pre>
  
  <p>Actual order by <code>order_id</code>: 1, 2, 3, 1</p>
  
  <p>Required order by <code>order_id</code>: 1, 3, 2, 1</p>