angularjsHow do I use the orderBy filter in AngularJS?
The orderBy filter in AngularJS is used to sort an array of objects by one or more of their properties. The syntax for using this filter is: {{ orderBy_expression | orderBy : expression : reverse}}.
The expression is a string that represents the property that you wish to sort by. The reverse is a Boolean value that determines whether the array should be sorted in ascending (false) or descending (true) order.
For example, the following code block sorts an array of objects by their name property in ascending order:
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="x in names | orderBy:'name':false">
{{x.name}}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = [
{name: 'John', age: 25},
{name: 'Jane', age: 30},
{name: 'Mary', age: 28}
];
});
</script>
The output of this code is:
John
Jane
Mary
Explanation
ng-repeat- Theng-repeatdirective is used to iterate over an array of objects and display its contents.orderBy- TheorderByfilter is used to sort an array of objects by one or more of their properties.expression- Theexpressionis a string that represents the property that you wish to sort by.reverse- Thereverseis a Boolean value that determines whether the array should be sorted in ascending (false) or descending (true) order.
Relevant Links
More of Angularjs
- How can I use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
- How do I use AngularJS to select an item from a list?
See more codes...