-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sortBy comparator handles arrays #1751
Conversation
|
Also this implementation prioritizes the specialized case (sorting by multiple properties) over the common case (not sure if there's a way around that other than a separate function or mixin which may be more appropriate anyway). |
Yeah this is very cool but probably not all that common. |
I've often found myself sorting by multiple fields more often than not in my code and it's not something particularly easy to mixin (a lot of cases to handle, can't call lookupIterator, etc). In these cases I find For the computing the value of the return rather than the parameter as with |
This would fit better as a more specialized method rather than trying to cram it into
Not surprised a small set of methods aren't appropriate for all scenarios.
Then expose it. I've exposed the helper as
The methods
The answer isn't to stuff more into these skinny jeans, you're gonna bust a zipper. |
Sure, but I'd argue that this is a common situation based on the number of tickets requesting the feature:
etc... and something core should support. This pr can be optimized further ofc for the common non array case. |
That doesn't mean |
What about a |
SMT like this maybe (quickly written untested) if you guys really are against switching on return values :/ function baseComparitor(left, right) {
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}
function arrayComparitor(left, right) {
var leftArr = left.criteria;
var rightArr = right.criteria;
for (var i = 0, len = leftArr.length, blen = rightArr.length; i < len; i++) {
if (i >= blen) return 1;
var a = leftArr[i], b = rightArr[i];
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
}
return blen == len ? left.index - right.index : -1;
}
function sortByComparitor(comparator) {
return function(obj, iterator, context) {
iterator = lookupIterator(iterator, context);
return _.pluck(_.map(obj, function(value, index, list) {
return {
value: value,
index: index,
criteria: iterator(value, index, list)
};
}).sort(comparator), 'value');
};
}
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = sortByComparitor(baseComparitor);
_.sortByArray = sortByComparitor(arrayComparitor); |
@megawac — you seem to be around this afternoon. Mind popping on IRC for a minute? |
Related to #2848. |
Fixes #1779, #1880
Another try at getting
sortBy
support for multiple criteria since #1359 was rejected. I'd argue this approach is more robust as its implementation is based on the return of the iterator instead of only handling multi prop lookups. Thus we can implement the same behaviour as #1359 with aproperties
mixin if desired later.Post implementation enables:
Exmple Usage (naive semver sort):
The thing that tripped me up implementing this is that the comparitor ranks
undefined > anything
. I'd argue thatundefined < anything
but that's just me.I'd really like to see this behaviour in v2.