title: Guava's ComparisonChain conversion to JavaScript
body: <p>I'm converting some code to JavaScript from Java, and I've come across this code:</p>
  
  <pre><code>static final Comparator&lt;Point&gt; compareXCoord = new Comparator&lt;Point&gt;() {
      @Override
      public int compare(Point o1, Point o2) {
          return ComparisonChain.start().
                  compare(o1.x, o2.x).
                  compare(o1.y, o2.y).
                  result();
      }
  };
  
  static final Comparator&lt;Point&gt; compareYCoord = new Comparator&lt;Point&gt;() {
      @Override
      public int compare(Point o1, Point o2) {
          return ComparisonChain.start().
                  compare(o1.y, o2.y).
                  compare(o1.x, o2.x).result();
      }
  };
  
  ...
  
  Arrays.binarySearch(ypoints, Point.make(Double.NEGATIVE_INFINITY, ymin),
              compareYCoord);
  </code></pre>
  
  <p>Seems fair enough, as it'll return the resultant object of the matching comparison chain. </p>
  
  <p>But what I don't understand is that both methods seem to my naive eyes to do the exact same thing: return if either the <code>x</code> or <code>y</code> properties match.  The only difference is the first checks the <code>x</code> property first, i.e., the order of the checks is different.</p>
  
  <p>Also, if that's true, then the <code>Arrays.binarySearch</code> method will return <code>ypoints</code> elements with equal <code>x</code> properties. I don't feel this is what the function is intended to do.</p>
  
  <p>So, my JavaScript translation would be:</p>
  
  <pre><code>function compareXCoord(p1, p2) {
    return (p1.x === p2.x) ? p1 : 
           (p1.y === p2.y) ? p1 : undefined;
  }
  
  function compareYCoord(p1, p2) {
    return (p1.y === p2.y) ? p1 : 
           (p1.x === p2.x) ? p1 : undefined;
  }
  </code></pre>
  
  <p>But both of these could be simplified to <code>return (p1.y === p2.y || p1.x === p2.x) ? p1 : undefined;</code>.</p>
  
  <p>I feel I'm certainly misunderstanding how <code>ComparisonChain</code> works. Is the order of the chain important? Bonus points for directions into how to translate this to JavaScript.</p>