Angular.js: ngRepeat: Sorting with a function works except with "track by"

Created on 23 Dec 2013  路  9Comments  路  Source: angular/angular.js

As you can see in this JsFiddle, http://jsfiddle.net/h4FCy/, if you sort a repeater with a function and a "track by", it doesn't sort items. If you remove the "track by", the items are sorted.

Most helpful comment

The syntax is tricky here. track by always goes at the end, even after other filters:

item in items | orderBy:'toString()' track by $index

See updated working fiddle: http://jsfiddle.net/VrLBz/1/

All 9 comments

The syntax is tricky here. track by always goes at the end, even after other filters:

item in items | orderBy:'toString()' track by $index

See updated working fiddle: http://jsfiddle.net/VrLBz/1/

I didn't know that, thanks. I thought at first I had a working exemple (with object instead of strings) but it was working by chance.

:+1: i was looking for this

:+1: awesome - thanks

Superb !! track by and order by worked after an hour of googling until i found this. thanks.

please document this in the docs for ng-repeat, i also had to search for a long time before i found this!

@CWSpear thanks!

@CWSpear thanks

I think I might have found one case where track by $index at the end fails to sort. If the filters are being set to a result.
x in (auditEventsFiltered = (auditEvents | orderBy:sortType:sortReverse | statusOutcomeFilter:filters | multiWordFilter:filters)) track by $index

Was this page helpful?
0 / 5 - 0 ratings