Creating AngularJS custom filter that actually Sort

前端 未结 1 797
借酒劲吻你
借酒劲吻你 2021-01-27 05:35

I know that in angularJS there is some built in filter: for example

  • But I don\'t want to us

  • 1条回答
    •  太阳男子
      2021-01-27 06:10

      Use Array.sort. Refer to the example below

      var app = angular.module('so', []);
      
      app.controller('MyCtrl', function($scope) {
        $scope.students = [{'name': 'student'}, {'name': 'john'}, {'name': 'austin'}, {'name': 'doe'}];
      });
      
      app.filter('filterByName', function () {
        return function (item) {
            return item.sort((a,b) => a.name.localeCompare(b.name))
        };
      });
      
      
      
    • {{student.name}}
    • 0 讨论(0)
    提交回复
    热议问题