0

I'm trying to alphabetize a list based on a json array, but I cant seem to get it to work. My current code looks like this. Here is a link to the jsfiddle http://jsfiddle.net/hxxLaxL3/

HTML

<div ng-app>
  <div ng-controller="Ctrl">
    <li ng-repeat="f in friends | orderBy:'f'">{{f}}</li>      
  </div>
</div>

Angular

function Ctrl($scope) {
  $scope.friends =
      ['C',
      'B',
      'Z',
      'S'];
}

Output

  • C
  • B
  • Z
  • S

Desired Output

  • B
  • C
  • S
  • Z
user3273370
  • 25
  • 1
  • 6
  • 2
    `$scope.friends = ['C', 'B', 'Z', 'S'].sort();` is probably easiest – Rhumborl Dec 01 '14 at 15:37
  • 1
    orderBy:'toString()' http://stackoverflow.com/questions/14493116/how-to-make-orderby-filter-work-on-array-of-strings – micha Dec 01 '14 at 15:53

1 Answers1

2

Change orderBy:'f' to orderBy:'toString()'

    <div ng-app>
      <div ng-controller="Ctrl">
        <li ng-repeat="f in friends | orderBy:'toString()'">{{f}}</li>      
      </div>
    </div>
Alexey Semenyuk
  • 3,263
  • 2
  • 32
  • 36