How to orderBy a array of objects in descending order in angularjs?
Solution 1
You should pass 2nd parameter as property name name
, then assign that filtered result to desire scope variable wherever you want. So there will not be any filtering needs to be done on UI ng-repeat
output.
$scope.person = $filter('orderBy')(person, 'name');
<div ng-repeat="p in person">
{{p.name}}
</div>
Forked Fiddle Here
If you wanted to see it on view you should keep that property on one of scope variable, or rather you can do this simple filtering on client side as well while displaying records.
<div ng-repeat="p in person | orderBy: 'name'">
{{p.name}}
</div>
Solution 2
Adding + or - prefix on orderBy parameter will order by + (ascending) or -(desc);
example:
<li ng-repeat="x in customers | orderBy : '-city'">
{{x.name + ", " + x.city}}
</li>
more at : http://www.w3schools.com/angular/ng_filter_orderby.asp
Or if you want to console.log it then just add name as parameter in quotations :
$filter('orderBy')(person, 'name');
Solution 3
Example:
<tr ng-repeat="friend in friends | orderBy:'-age'">
<td>{{friend.age}}</td>
<td>{{friend.name}}</td>
</tr>
Use '-' Symbol Before your expression
Comments
-
Saras Arya almost 2 years
I have a very simple array of objects and I want to sort it using
$filter('orderBy')
in javascript. Somehow it doesn't seem to work. jsFiddleHere is the code
var app = angular.module('myApp', []); app.controller('myController', function($scope, $filter){ var person = [{ name : "Saras" }, { name : "Arya" }]; $filter('orderBy')(person, 'name'); console.log(person); });
I don't understand why I can't get this to work? Help is appreciated. And the solution should be in JS not in HTML.
-
Saras Arya almost 8 yearsIt doesn't work... Could you post the link of a working fiddle... From the one given in question.
-
Pankaj Parkar almost 8 yearsI have updated my answer, could you please look into the fiddle which I have in my answer?
-
Saras Arya almost 8 yearsCan't the $scope.person array be modified in the controller file?
-
Pankaj Parkar almost 8 years@SarasArya yes, it can be modified from controller
-
Pankaj Parkar almost 8 years@SarasArya here you go, find fiddle here
-
Saras Arya almost 8 yearsOkay, so the mistake was. I was thinking that $filter('orderBy') would change the array itself. It didn't you have to get the output in another variable which does the trick. Is it right?
-
Pankaj Parkar almost 8 yearscorrect. that will return a new array which is filtered one, you have to reassign back to the desired variable again to see the changes
-
Sumit Joshi over 6 yearsif you want to sort in descending order then set true in third parameter like this: $scope.person = $filter('orderBy')(person, 'name', true);
-
Dudi over 6 yearsU can also use ! : <li ng-repeat="x in customers | orderBy : '!city'">
-
Artjom B. over 5 yearsHi Muhammad, can you explain what your answer provides in addition to the already given answers?
-
Muhammad Tanvir Mehthaf over 5 years@ArtjomB. I give that what expression should have to use beside value . for clarification . i give comments
-
Harshal Yelpale almost 5 years@PankajParkar That's a perfect answer(both ways), it works well!!. However, for descending order covered by SumitJoshi. Thanks.