AngularJS Tutorial - orderBy Filter








The orderBy filter sorts the array using an expression.

The orderBy function takes two parameters. First one is required, the second is optional.

The first parameter is the predicate used to determine the order. It can be

  • function - will use the function as the getter function for the object.
  • string - will parse the string and use the result as the key to order the array. We can pass either a + or a - to force the sort in ascending or descending order.
  • array - will use the elements as predicates in the sort expression.

The second parameter controls the sort order of the array: reversed or not.





Example

To sort an array of objects by their name

{{  [{
       'name': 'A',
       'status': 'ok'
     },  {
       'name': 'Q',
       'status':  'notOK'
     },  {
       'name': 'Z',
       'status': 'ok'
     }] | orderBy: 'name'  
}}

To reverse-sort the object, add the second parameter as true:

{{  [{
       'name': 'A',
       'status': 'ok'
     },  {
       'name': 'Q',
       'status':  'notOK'
     },  {
       'name': 'Z',
       'status': 'ok'
     }] | orderBy: 'name':true  
}}