- Various Types of Form Validation in AngularJS
- Double Click Event in AngularJS
- Mouse/Pointer Events in AngularJS
- Cut and Copy Events in AngularJS
- Paste Event in AngularJS
- Contact Book Using AngularJS
- Change Background Color Dynamically Using AngularJS
Now in this article I will describe sorting and OrderBy filtering in AngularJS.
I assume everyone knows that sorting is the process of arranging items depending on a specified sequence.
AngularJS Official Wbsite.
<!doctype html>
<html ng-app>
<head>
<script src="angular.min.js"></script>
<script>
function Ctrl($scope) {
$scope.friends = [{sno:1, name:'Sourabh Somani', Marks:'75'},
{sno:2, name:'Shaili Dashora', Marks:'90'},
{sno:3, name:'Divya Sharma', Marks:'89'},
{sno:4, name:'Swati Soni', Marks:'78'},
{sno:5, name:'Ankit', Marks:'74'}]
}
</script>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Note: "mystyle.css" file you can download from uploaded Zip File.
<body>
<div ng-controller="Ctrl">
<table cellspacing='0'>
<thead>
<tr>
<th><center><a href="" ng-click="predicate = 'sno'; reverse=!reverse">S.NO</a></center></th>
<th><center><a href="" ng-click="predicate = 'name'; reverse=!reverse">NAMES</a></center></th
<th><center><a href="" ng-click="predicate = 'Marks'; reverse=!reverse">MARKS</a></center></th>
</tr>
</thead>
- Function: The '<', '>', '=' operator decides the result of the function.
- String: One of the best properties of AngularJS is to order by 'name'. The '+' and '-' prefixes are used for ascending and descending order respectively, for example '+name' or '-name'. This is the easiest way of sorting.
- Array: Suppose two item values are equivalent like marks of the two students (A and B) are the same like 95% and we are sorting by percentage (%), then there might confusion about whether the student name should be first, so for that the next predicate is used. So we can say an Array may me a Function predicate or a String predicate.
<tbody>
<tr ng-repeat="friend in friends | orderBy:predicate:reverse">
<td>{{friend.sno}}</td>
<td>{{friend.name}}</td>
<td>{{friend.Marks}}%</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
<!doctype html><!--Root Elemet-->
<html ng-app>
<head>
<script src="angular.min.js"></script>
<title>Sorting using AngularJS</title>
<script>
function Ctrl($scope) {
$scope.friends =[{sno:1, name:'Sourabh Somani', Marks:'75'},
{sno:2, name:'Shaili Dashora', Marks:'90'},
{sno:3, name:'Divya Sharma', Marks:'89'},
{sno:4, name:'Swati Soni', Marks:'78'},
{sno:5, name:'Ankit', Marks:'74'}]
}
</script>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<div ng-controller="Ctrl">
<H1>Sorting The List In AngularJS</H1><hr/>
<table cellspacing='0'>
<thead>
<tr>
<th><center><a href="" ng-click="predicate = 'sno'; reverse=!reverse">S.NO</a></center></th>
<th><center><a href="" ng-click="predicate = 'name'; reverse=!reverse">NAMES</a></center></th>
<th><center><a href="" ng-click="predicate = 'Marks'; reverse=!reverse">MARKS</a></center></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="friend in friends | orderBy:predicate:reverse">
<td>{{friend.sno}}</td>
<td>{{friend.name}}</td>
<td>{{friend.Marks}}%</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Output
- When the page loads:

- Sorting using Serial Number:


- Sorting by names:


- Sorting by marks:



Yashwanth MuthineniPosted Aug 31, 2015, 8:37 AM
Nice Share
Debendra DashPosted Jun 22, 2015, 5:12 AM
good one sir........