Use AngularJS OrderBy Filter in Controller


We demonstrated in my previous article 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.

Kailash Chandra Behera


  1. Share great information about your blog , Blog really helpful for us . Hosted Phone Systems

  2. Thanks for sharing this article here about the church book publishing services. Your article is very informative and I will share it with my other friends as the information is really very useful. Keep sharing your excellent work.

    ecommerce catalogue management services

    ecommerce services provider in india

  3. How to bet on esports games: Top 10 tips for
    The most popular esports betting strategies include: With the popularity of eSports in South Korea, it's important to note kirill-kondrashin that the 카지노 major game