如何通过过滤器对字符串数组进行排序?

Sun*_*hah 84 angularjs

这是不起作用的代码:演示:http://jsfiddle.net/8dt94/63/

<div ng-controller="MyCtrl">    
    <input type="text" ng-model="searchText" />
  <ul ng-repeat="strVal in arrVal|orderBy|filter:searchText" >
      <li>{{strVal}}</li>
  </ul>
</div>

var app=angular.module('myApp', []);
app.controller('MyCtrl', function ($scope,$filter) {
  $scope.arrVal = ['one','two','three','four','five','six'];  
});
Run Code Online (Sandbox Code Playgroud)

not*_*ive 246

您可以通过方法进行排序,因此可以使用toString方法

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案,我需要一个像这样排序的数组| [2,5,3,1,6,33]`所以相反f`toString()`我使用`valueOf()`它完美无缺.谢谢你的解决方案. (9认同)

Mar*_*cok 11

编写自定义过滤器:

app.filter('mySort', function() {
    return function(input) {
      return input.sort();
    }
  });
Run Code Online (Sandbox Code Playgroud)

HTML:

<ul ng-repeat="strVal in arrVal|filter:searchText|mySort">
Run Code Online (Sandbox Code Playgroud)

小提琴.