Gobli
Gobli

Reputation: 347

AngularJs: The expression of the orderBy doesnt work to me

I'm using AngularJs 1.2.8. Here's my question:

I want to order an array of objects using a filter, and a strange thing happens to me.

When I use the ng-repeat="thread in threads" without any filter it orders it by ascendant.

When I use the ng-repeat="thread in threads | orderBy: '-':true" it orders it by descendant.

Here's the problem: when I use ng-repeat="thread in threads | orderBy: '-':false" it orders it by descendant anyway.

Thank you all.

Upvotes: 0

Views: 139

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38787

Are you trying to order your list based on a certain property? Usually orderBy is used in relation to a property within each item in ng-repeat:

Descending based on an example property title

ng-repeat="thread in threads | orderBy: '-title'"

ng-repeat="thread in threads | orderBy: '-date'"

ng-repeat="thread in threads | orderBy: '-id'"

Ascending based on an example property title

ng-repeat="thread in threads | orderBy: '-title'"

ng-repeat="thread in threads | orderBy: '+date'"

ng-repeat="thread in threads | orderBy: '+id'"

Angular documentation on orderBy. Notice the '+' or '-' are affixed to beginning of property name within the single quote characters.

You can also use in combination with a custom filter named query, perhaps on a text input element with ng-model="query". For example:

ng-repeat="thread in threads | filter:query | orderBy: '-title'"

Let me know if that helps or clarify question to identify properties of thread entity and what property you are hoping to sort on.

to order on index:

Descending ng-repeat="thread in threads | orderBy: thread.$index:true"

Ascending ng-repeat="thread in threads | orderBy: thread.$index:false"

jsFiddle:

http://jsfiddle.net/g9posLyp/

Upvotes: 1

Related Questions