Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make orderby filter work on array of strings?

Tags:

angularjs

Here is the code which is not working: Demo: 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'];   }); 
like image 438
SunnyShah Avatar asked Jan 24 '13 02:01

SunnyShah


People also ask

Which built in filter can you use to sort an array Angular?

An orderBy Filter in AngularJS is used to sort the given array to the specific order.

What is orderBy filter?

Definition and Usage The orderBy filter allows us to sort an array. By default, strings are sorted alphabetically, and numbers are sorted numerically.


2 Answers

You can order by a method, so you can use the toString method

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText"> 
like image 152
notclive Avatar answered Dec 09 '22 05:12

notclive


Write a custom filter:

app.filter('mySort', function() {     return function(input) {       return input.sort();     }   }); 

HTML:

<ul ng-repeat="strVal in arrVal|filter:searchText|mySort"> 

Fiddle.

like image 34
Mark Rajcok Avatar answered Dec 09 '22 05:12

Mark Rajcok