How to make orderby filter work on array of strings?

Angularjs

Angularjs Problem Overview


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'];  
});

Angularjs Solutions


Solution 1 - Angularjs

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

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

Solution 2 - Angularjs

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSunnyShahView Question on Stackoverflow
Solution 1 - AngularjsnotcliveView Answer on Stackoverflow
Solution 2 - AngularjsMark RajcokView Answer on Stackoverflow