I've seen lots of answers and documentation indicating that you can pass an array of attribute names to Angular's orderBy filter to sort by multiple fields. However, one of my object attributes is a number stored as text (field can be "100", "75", "50", or "<25"). In order to sort these values as numbers, I have been using:
<div ng-repeat="item in items | orderBy:sortFunction">
{{item.itemStatus}} - {{item.itemLevel}}
</div>
//Function in controller...
$scope.sortFunction = function(item) {
return parseInt(item.itemLevel);
}
What I want to do now is sort first by item.itemStatus, then by item.itemLevel as a number. Is there a way to do that without first changing my data model? I could loop through all items and add a new attribute that is the itemLevel as a number, then filter using
ng-repeat="item in items | orderBy:['itemStatus','itemLevelAsNumber']
but I would like to know if there's a better way to do it. I have tried
ng-repeat="item in items | orderBy:['itemStatus',sortFunction]
but I think using a function in that scenario expects to have the attribute name returned as a string.
Thanks!
item in items | orderBy:['itemStatus',sortFunction]should be fine. Proved by this fiddle