java - Guava's ComparisonChain conversion to JavaScript -
i'm converting code javascript java, , i've come across code:
static final comparator<point> comparexcoord = new comparator<point>() { @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<point> compareycoord = new comparator<point>() { @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); seems fair enough, it'll return resultant object of matching comparison chain.
but don't understand both methods seem naive eyes exact same thing: return if either x or y properties match. difference first checks x property first, i.e., order of checks different.
also, if that's true, arrays.binarysearch method return ypoints elements equal x properties. don't feel function intended do.
so, javascript translation be:
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; } but both of these simplified return (p1.y === p2.y || p1.x === p2.x) ? p1 : undefined;.
i feel i'm misunderstanding how comparisonchain works. order of chain important? bonus points directions how translate javascript.
you need bear in mind java comparator return value three-valued (i.e. higher, lower, or equal). order in compare coordinates important - sorting x first not same sorting y first.
Comments
Post a Comment