orderBy

  1. filter in module ng

Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers. Note: if you notice numbers are not being sorted as expected, make sure they are actually being saved as numbers and not strings. Array-like values (e.g. NodeLists, jQuery objects, TypedArrays, Strings, etc) are also supported.

Usage

In HTML Template Binding

{{ orderBy_expression | orderBy : expression : reverse}}

In JavaScript

$filter('orderBy')(array, expression, reverse)

Arguments

Param Type Details
array Array

The array (or array-like object) to sort.

expression function(*)stringArray.<(function(*)|string)>=

A predicate to be used by the comparator to determine the order of elements.

Can be one of:

  • function: Getter function. The result of this function will be sorted using the <, ===, > operator.
  • string: An Angular expression. The result of this expression is used to compare elements (for example name to sort by a property called name or name.substr(0, 3) to sort by 3 first characters of a property called name). The result of a constant expression is interpreted as a property name to be used in comparisons (for example "special name" to sort object by the value of their special name property). An expression can be optionally prefixed with + or - to control ascending or descending sort order (for example, +name or -name). If no property is provided, (e.g. '+') then the array element itself is used to compare where sorting.
  • Array: An array of function or string predicates. The first predicate in the array is used for sorting, but when two items are equivalent, the next predicate is used.

    If the predicate is missing or empty then it defaults to '+'.

reverse
(optional)
boolean

Reverse the order of the array.

Returns

Array

Sorted copy of the source array.

The example below demonstrates a simple ngRepeat, where the data is sorted by age in descending order (predicate is set to '-age'). reverse is not set, which means it defaults to false.

The predicate and reverse parameters can be controlled dynamically through scope properties, as shown in the next example.

It's also possible to call the orderBy filter manually, by injecting $filter, retrieving the filter routine with $filter('orderBy'), and calling the returned filter routine with the desired parameters.

Example:

doc_AngularJS
2016-03-29 16:12:35
Comments
Leave a Comment

Please login to continue.