javascript - AngularJS Custom Filter orderBy sortPredicate not working -
the problem have array of recipe objects. each recipe object has comments on it. want sort array in angularjs controller using $filter service provided angular.
$scope.recipes = $filter('orderby')($scope.data, function(recipe) { return recipe.comments.length; });
but not giving required results. however, i'm able achieve desired results using js array sort functionality this
$scope.data.sort(function(a, b) { if (a.comments.length < b.comments.length) return 1; if (b.comments.length < a.comments.length) return -1; return 0; });
the plunkr same scenario : http://plnkr.co/edit/l9bt67xhrcjlbowg8ezp?p=preview
thanks in advance. please help!
it can done lot simpler using orderby
http://plnkr.co/edit/b0fmi7fotgmg2tkcjyst?p=preview
<ul> <li ng-repeat="r in recipes | orderby:'-comments.length'"> {{r.title}} - {{r.comments.length}} </li> </ul>
Comments
Post a Comment