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
Sort order with multiple keys #414
Comments
Thanks for the encouragement! I'll keep a tab on this to see if it's requested more, but at the moment I don't have plans to expand this to |
+1 - would definitely like this! Syntax could be similar to angular's orderBy filter: https://docs.angularjs.org/api/ng/filter/orderBy, where you'd simply put a '+' (optional) or '-' in front of the property name to control ascending/descending. |
@jdalton [{'foo': 'A', 'bar': 1}, #1
{'foo': 'A', 'bar': 2}, #2
{'foo': 'B', 'bar': 1}, #3
{'foo': 'B', 'bar': 2}] #4 If I do a reverse, or use right associative methods, I can only get 4-3-2-1. As @joshbowdoin suggests, an optional '-' which indicates descending would be a really nice addition. BTW, really nice work. It's saved me tons of hours. |
+1 - would love a _.sortBy(myArray, ['foo', '-bar']) syntax. |
+1 |
I'd dig a proposed implementation to go along with the +1's. |
+1 for using an order prefix for each sort |
You always can add _.sortBySome |
I'd love to see Perhaps a signature something like Copying & adapting
What I like about this approach, using an order-indicating array side-by-side with the criteria array, is that it can easily default to all-ascending when left unspecified/empty. It also doesn't restrict the names of properties like the The thing I couldn't figure out is correctly modifying I get tripped up determining whether the iteratee-check area needs any changes, and then also around the call to Anyway, it'd be great to have something like this working for |
I'd prefer an options object, maybe |
Allow props array to optionally contain objects with dir as a value? var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 26 },
{ 'user': 'fred', 'age': 30 }
];
// default to standard direction
_.sortByAll(myArray, ['user', 'age'])
or
// define direction
_.sortByAll(myArray, [{user: 1}, {age: -1}]) |
Crawling keys is not ideal. |
I think for the sake of consistency, this might be better:
iteratee / predicate style, what do you think @jdalton ? |
I'll accept a PR for |
Sounds good. I guess I'll take a stab at that tonight. |
Is a sort object an issue since property order of objects are not guaranteed in JavaScript? Could
|
Right. I guess we'd have to type the long |
Maybe to streamline ui implementation consider "reverse: bool" instead of "dir: string". _.sortByAll(myArray, [{key: "user"}, {key: "age", reverse: true}]) $scope.sort = [{key: 'user'}, {key: 'age', reverse: true}] <ng-repeat="option in sort">
<caret ng-click="option.reverse = !option.reverse"></caret>
<label>{{::option.key}}</label>
</ng-repeat> |
Since sorting is ascending by default how about a |
^^ +1 for desc: true/falsey |
I'm good with
|
Keep in mind it would only be needed when different than the default. So regular keys could be used for the common case. |
Implement optional descending order sort in #414.
Hello @jdalton. Is there any way to pass values with keys in |
I'm not sure what you're trying to accomplish. There's _.sortByOrder which allows specifying booleans for sort order direction. |
I'm responsible for upgrading lodash to 3.8 version from 2.4.1. Where I came to this code where I thought I can improve code. Example: var arr = [{"name" : "test' , type : "1",isActive :true},{"name" : "tea', type : "2" , isActive :false},{"name" : "tea' , type : "1", isActive :true}]; Now I want to sort first with _.sortBy(_.sortBy(arr, {"type" : 1}), {"isActive" : false})); Can I use _.sortByAll(arr, {"type" : 1, "isActive" : false}); but this didn't work for me. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi, I am a in awe of the power of lodash. For my project it has been of invaluable help so far. However, now I am stuck with a problem with which lodash seems to fall a bit short.
Here is the problem:
I have an array of objects with the same properties, say
I want to sort the array by name in descending order, but age should be in ascending order.
lodash supports multiple sorting keys by the "pluck" syntax, but only can deal with ascending sorting this way. Is there a way that the sortBy function is altered that it also accepts an array of objects that define the property name (key) and the order direction? I would love to do:
Any chance something like that enters the library?
The text was updated successfully, but these errors were encountered: