How can I get Angular's ng-repeat
directive to sort a list by each item's actual value, rather than by the value of a property on each item?
eg:
<ul> <li ng-repeat="item in items | orderBy:'WHAT_GOES_HERE??'">{{item}}</li> </ul>
Here's a fiddle to play with: http://jsbin.com/okatur/1/edit
I realize I could just do .sort()
on the array, but is that my only option?
Since AngularJS 1.3.0-rc.5, the orderBy
filter (see the documentation) will automatically sort the array using its items if no additional parameters are provided.
<li ng-repeat="item in items | orderBy">{{item}}</li>
JS Bin
The orderBy
filter (see the historical documentation) can also take a function as second parameter, whose return value will be compared using the <
, =
and >
operator. You can simply use the angular.identity
(see the documentation) for that purpose:
$scope.identity = angular.identity;
<li ng-repeat="item in items | orderBy:identity">{{item}}</li>
JS Bin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With