AngularJS Orderby Filter

Kailash Chandra Behera | Tuesday, February 07, 2017


We demonstrated in my previous blog how to use orderBy filter in HTML. This article demonstrates how to use orderBy filter in JavaScript with controller.

Getting Started

This example is doing same things like the example in my previous article, but uses $filter() method for sorting table data. The sort button sorts the table record, it contains a function called sortBy() which sorts data using $filter() method. sortBy function takes selected item from DropdownBox which contains list of column names for sorting and calls $filter(). For details see the below example.


 <HTML ng-app = "myapp">  
      <TITLE> AngularJS learning(orderBy Filter)</TITLE>  
      <script src="angular.min.js"></script>  
           var myapp=angular.module("myapp",[]);  
           $scope.students = $filter('orderBy')($scope.students, $scope.colname,true);  
                     $scope.students = $filter('orderBy')($scope.students, $scope.colname,false);  
      <BODY ng-controller="myappcont">  
           <h2>Student Details</h2>  
           <table border="1">  
           Sort By : <select ng-model="colname">  
           <option value="Name">Name Asc</option>  
           <option value="-Name">Name Desc</option>  
           <option value="Sex">Sex Asc</option>  
           <option value="-Sex">Sex Desc</option>  
           <option value="Class">Class Asc</option>  
           <option value="-Class">Class Desc</option>  
           <option value="Section">Section Asc</option>  
           <option value="-Section">Section Desc</option>  
           </select>  <button ng-click="sortBy()">Sort</button>  
                <tr ng-repeat="student in students">  


This article demonstrates how to use $filter() or orderBy in controller, hope this article my helpful to you.