Implement sortByComparison #112

Closed
aadamowski opened this Issue Mar 12, 2012 · 0 comments

Comments

Projects
None yet
2 participants
Contributor

aadamowski commented Mar 12, 2012

Currently, the sortBy method requires that an iterator is provided that returns a numerical value to be used for sorting elements according to numerical order.

This requirement cannot always be easily satisfied - the programmer is burdened with devising a proper numeric space which would be homomorphic to the sorted set with respect to its elements order.

I propose implementing additional sorting function, "sortByComparison", which in many cases would provide for cleaner and easier sorting.

The sortByComparison function should implement merge sort, possibly parallelized, and delegate element comparison to the iterator supplied to it.

Obviously, the iterator (or, for a better name, comparator) function should take not one, but two elements as its arguments and, similarly to its Java analog, should supply the comparison result to the callback function:

if (1st argument < 2nd argument) {
  callback(null, any integer < 0);
} else if (1st argument > 2nd argument) {
  callback(null, any integer  > 0);
} else if (1st argument == 2nd argument) {
  callback(null, 0);
} else {
  callback(new Error('this is impossible'), null);
}

@caolan caolan closed this Mar 28, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment