How to orderBy a array of objects in descending order in angularjs?

49,321

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>

Demo here

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

Share:
49,321
Saras Arya
Author by

Saras Arya

Coder.

Updated on August 06, 2022

Comments

  • Saras Arya
    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. jsFiddle

    Here 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
    Saras Arya almost 8 years
    It doesn't work... Could you post the link of a working fiddle... From the one given in question.
  • Pankaj Parkar
    Pankaj Parkar almost 8 years
    I have updated my answer, could you please look into the fiddle which I have in my answer?
  • Saras Arya
    Saras Arya almost 8 years
    Can't the $scope.person array be modified in the controller file?
  • Pankaj Parkar
    Pankaj Parkar almost 8 years
    @SarasArya yes, it can be modified from controller
  • Pankaj Parkar
    Pankaj Parkar almost 8 years
    @SarasArya here you go, find fiddle here
  • Saras Arya
    Saras Arya almost 8 years
    Okay, 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
    Pankaj Parkar almost 8 years
    correct. 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
    Sumit Joshi over 6 years
    if you want to sort in descending order then set true in third parameter like this: $scope.person = $filter('orderBy')(person, 'name', true);
  • Dudi
    Dudi over 6 years
    U can also use ! : <li ng-repeat="x in customers | orderBy : '!city'">
  • Artjom B.
    Artjom B. over 5 years
    Hi Muhammad, can you explain what your answer provides in addition to the already given answers?
  • Muhammad Tanvir Mehthaf
    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
    Harshal Yelpale almost 5 years
    @PankajParkar That's a perfect answer(both ways), it works well!!. However, for descending order covered by SumitJoshi. Thanks.