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
2
  • 2
    $scope.friends = ['C', 'B', 'Z', 'S'].sort(); is probably easiest Commented Dec 1, 2014 at 15:37
  • 1
    orderBy:'toString()' stackoverflow.com/questions/14493116/… Commented Dec 1, 2014 at 15:53

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.