0

Is there a way that I can arrange a simple array like below:

<div ng-init="fruits=['apple','orange','mango','banana','pineapple','kiwi']">

in ascending order on page load

I tried this, but it dint work:

<ul>
<li ng-repeat="fruit in fruits | orderBy:predicate='fruit'">{{fruit}}</li>
</ul>

I'm sure there must be some pretty simple ways but since I'm a novice in Angular, would like help on this.

0

1 Answer 1

1

predicate='fruit' is not a valid orderBy expression (at least not a meaningful one, anyway). To orderBy items in an array as strings, use 'toString()' as the predicate...

<ul>
    <li ng-repeat="fruit in fruits | orderBy:'toString()'">{{fruit}}</li>
</ul>

JsFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Great! I've learnt something new and I'm happy about that! Thanks Anthony Chu :)

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.