AngularJS Tutorial - Custom function for orderBy filter








The following code shows how to create custom function for orderBy filter.

Example


<!doctype html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  </head><!--from   w w w.  j a  va  2s  . com-->
<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <p ng-repeat="i in list|orderBy:random">{{i}}</p>
</div>
<script type='text/javascript'>
var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
    $scope.random = function() {
        return 0.5 - Math.random();
    }
}
</script>
</body>
</html>

The code above is rendered as follows: