The problem is that I have an array of recipe objects. Each recipe object has some comments on it. I want to sort the array in angularJS controller using the $filter service provided by angular.
$scope.recipes = $filter('orderBy')($scope.data, function(recipe) {
return recipe.comments.length;
});
But its not giving the required results. However, I'm able to achieve the desired results using the JS array sort functionality like 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 for the same scenario is : http://plnkr.co/edit/L9Bt67xHRCJLBoWG8EZp?p=preview
Thanks in advance. Please Help!
It can be done a 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>