import java.util.Comparator;

public class ByWeightComparator implements Comparator
   {

   /**
   * Compare two Trucks by weight. Callback for sort or TreeMap.
   * effectively returns a-b; orders by ascending weight
   *
   * @param pFirst first object a to be compared
   *
   * @param pSecond second object b to be compared
   *
   * @return +1 if a>b, 0 if a=b, -1 if a<b
   */
   public final int compare ( Object pFirst, Object pSecond )
      {
      long aFirstWeight = ( (Truck)pFirst ).weight;
      long aSecondWeight = ( (Truck)pSecond ).weight;
      /* need signum to convert long to int, (int)will not do! */
      return signum( aFirstWeight - aSecondWeight );
      } // end compare
   /**
   * Collapse number down to +1 0 or -1 depending on sign.
   * Typically used in compare routines to collapse a difference
   * of two longs to an int.
   *
   * @param diff usually represents the difference of two long.
   *
   * @return signum of diff, +1, 0 or -1.
   */
   public static final int signum ( long diff )
      {
      if ( diff > 0 ) return 1;
      if ( diff < 0 ) return -1;
      else return 0;
      } // end signum

   } // end class ByWeight