Why does the "orderBy` filter in AngularJS ignore the" comparator "parameter?

I am trying to arrange an array of objects using the comparator function, but it seems that the comparator function is completely ignored (see the angular documentation).

I am using angularJS 1.5.6.

Here is the JSFiddle

Html:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"> </script> <body ng-app="app" ng-controller="ctrl"> {{msg}} </body> 

JavaScript:

 angular.module("app", []) .factory('f1', function($filter) { var f1 = {}; function comparator(a,b) { console.log(a,b); return a.id - b.id; } function getter(x) { /*console.log(x);*/ return x; } f1.testOrderBy = function() { return $filter('orderBy')( [ {id:3}, {id:1}, {id:2} ], getter, false, comparator ) .map(function(x) { return x.id; }) }; return f1; }) .controller("ctrl", function($scope, f1) { $scope.msg = f1.testOrderBy(); }) 

My question is : Why is comparator ignored? (This can be seen since the console.log() call is never executed). Is this a corner mistake?

Because of this, I can’t even order an array of objects using a specialized comparator.

Thanks!

+6
source share
1 answer

Since support for custom comparators in orderBy was added in 1.5.7 .

Here you can read the change log.

If you check the documentation for 1.5.6 , you will see that the api is described as:

 $filter('orderBy')(array, expression, reverse) 

In 1.5.7 :

 $filter('orderBy')(collection, expression, reverse, comparator) 
+11
source

All Articles