Search code examples
javascriptangularjsangularjs-ng-repeatangularjs-orderby

AngularJS: Sorting Strings With Spaces Using orderBy


I have an ng-repeat going, wherein:

<a ng-repeat="contact in contacts | orderBy:'name'">
{{contact.name}}
</a>

I have tried predicates: name, -name, name.substr(0, 3)

The contacts 'name' property is like "John Smith", "Betty Ford", "Hawkeye Pierce", etc.

The ng-repeat works, but the orderBy does not.

Is there a reason that my strings might not be sorting successfully?


Solution

  • 1 : The orderBy only works with Arrays -- See http://docs.angularjs.org/api/ng.filter:orderBy

    source : Angular - Can't make ng-repeat orderBy work

    What is your 'contacts' ?

    2 : try

    <div ng-repeat="contact in contacts | orderBy:'name'">
    <a>{{contact.name}}</a>
    </div>