87

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'];  
});
2
  • I do not believe you should be using primitive values in your ng-repeat array. If not it works. (jsfiddle.net/EGVwG). Commented Jan 24, 2013 at 3:38
  • This question still applies to the ng-options attr of a select, which must be a list of strings. Commented Jul 14, 2013 at 19:57

2 Answers 2

251

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

<ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText">
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Additionally you can add "track by" to the end: <ul ng-repeat="strVal in arrVal | orderBy:'toString()' | filter:searchText track by $index">
Great solution, I needed an array of numbers sorted like this [2,5,3,1,6, 33] so instead f toString() I used valueOf() and it worked perfect. Thanks for the solution.
This worked for me too! Any idea why this trick works though?
12

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.