0

just started Angularjs, I have a simple application where orderBy is not working. What is wrong here?

<body ng-init="names=['Ruby','Java','Oracle','Basic']">
<input type="text" data-ng-model="name"/>
<ul>
    <li ng-repeat="name in names | orderBy:'name' | filter:name">{{name | uppercase}}</li>
</ul>
<script type="text/javascript" src="js/angular.js"></script>
</body>

3 Answers 3

1

The orderBy filter will order an array of objects by a property within each member of the array. So if you say orderBy: 'name' then each member of the array should have a property called name in it. In your array there are no properties of name because your array contains Strings not Objects. If your array looked like:

names = [ { name: 'Ruby' }, { name: 'Java' }, { name: 'Oracle' }, { name: 'Basic'} ]

Then it'd work. Or you can change orderBy: 'name' to orderBy like so:

<li ng-repeat="name in names | orderBy | filter:name">{{name | uppercase}}</li>

Then it won't use a property of the member to order by and instead use the value of each member to order the array which is what you want with Strings.

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

2 Comments

How do I then do reverse orderBy?
You could try orderBy:null:true that's pretty close to what orderBy would cause. Unfortunately the docs do not specify how to work with non-object types so even though that what I wrote works, its undocumented.
1

The orderBy only works with arrays of objects -- See http://docs.angularjs.org/api/ng.filter:orderBy

I made a JSfiddle for you http://jsfiddle.net/aBccw/

$scope.names =[{name:'Ruby'}, {name:'test'},{name:'Oracle'},{name:'Basic'}];

Hope it helps

Comments

0

Answering my own question. Removed single quote from name and it started working. But not sure if I should stop using quotes?

<li ng-repeat="name in names | orderBy:name | filter:name">{{name | uppercase}}</li>

Comments

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.