I have this collection from DataBase:
var items = [{ \'Name\':\'Michael\', \'TypeId\':1 }
{ \'Name\':\'Max\', \'TypeId\':1 }
{ \'Name\':\'Andre\'
You could use orderBy
filter.
var itemsSorted = $filter('orderBy')(items, 'TypeId')
On View
ng-repeat="item in items | orderBy: 'TypeId'"
By default filters are ascending(explicit would be +TypeId
), you could use -TypeId
to make it descending.
Additional Stuff
If you wanted to sort by multiple properties then do use array instead of string
like ['TypeId', 'Name']
ng-repeat="item in items | orderBy: ['TypeId', 'Name']"
There is big performance benefit you get when you do manual filtering inside controller. where as filtering on view is slower as it evaluates ng-repeat
express and bindings each time when digest cycle fire. Generally you won't see any performance hit in small collection, but in bigger collection you will see filtering on view will work slow.