Angularjs quick filter sort an array by strings.
Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<div ng-controller="MyCtrl"> <input type="text" ng-model="searchText" /> <ul ng-repeat="strVal in arrVal|filter:searchText|mySort" > <li>{{strVal}}</li> </ul> </div> var app=angular.module('myApp', []); app.controller('MyCtrl', function ($scope,$filter) { $scope.arrVal = ['one','two','three','four','five','six']; }); app.filter('mySort', function() { return function(input) { return input.sort(); } }); |
You can also order by a method, so you can use the toString method
1 2 3 |
<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText"> |
Nice one for pointing out that you can sort by an existing method like that. It seems so obvious now I’ve seen it.
No Worries 🙂