-
Notifications
You must be signed in to change notification settings - Fork 331
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
orderBy not working with groupBy #26
Comments
@arturokunder Do you means orderBy ? |
Yeah! i meant orderBy. |
@arturokunder $scope.groups = [
{ category: 'alpha', id: 2 },
{ category: 'beta', id: 3 },
{ category: 'gamma', id: 0 },
{ category: 'alpha', id: 4 },
{ category: 'beta', id: 5 },
{ category: 'gamma', id: 1 }
];
// retrieves the min 'id' of a collection, used for the group ordering.
//you can use lodash instead. e.g: _.min(arr, 'id')
$scope.min = function(arr) {
return $filter('min')
($filter('map')(arr, 'id'));
} <ul ng-repeat="group in groups | groupBy:'category' | toArray:true | orderBy:min">
<!-- print the group name -->
<li>{{ group.$key }}</li>
<!-- iterate over the group members and order each group by id -->
<li ng-repeat="item in group | orderBy:'id'">
{{ item }}
</li>
</ul> RESULT:
|
Here's a fiddle: link |
Also, Ariel's suggestion is more performant this way (assuming your list is unordered to start). order-by is an O(N*logN) operation. Group-by is O(N). By transposing the two (group-by, then order-by), the second and more expensive operation runs (on average) on a small subset of the items. That is, N items to be sorted in each order-by will be much smaller within each Group. |
While trying this approach I experienced a certain performance issue as a side effect. The custom groupBy filter from http://sobrepere.com/blog/2014/10/14/creating-groupby-filter-angularjs/ worked better. |
@a8m cheers, glad it's on the way. |
` |
When you make a groupBy, filterBy is not working
For example:
if you make
It's render:
a
alpha
beta
b
gamma
instead of
b
gamma
a
beta
alpha
The text was updated successfully, but these errors were encountered: