How to sort by using multiple fields at same time in angular? fist by group and then by sub-group for Example
$scope.divisions = [{'group':1,'sub':1}, {'group':2,'sub':10}, {'group':1,'sub':2},{'group':1,'sub':20},{'group':2,'sub':1},
{'group':2,'sub':11}];
I wanted to display this as
group : Sub-group
1 - 1
1 - 2
1 - 20
2 - 1
2 - 10
2 - 11
<select ng-model="divs" ng-options="(d.group+' - '+d.sub) for d in divisions | orderBy:'group' | orderBy:'sub'" />
There are 2 ways of doing AngularJs filters, one in the HTML using {{}} and one in actual JS files...
You can solve you problem by using :
if you use it in the HTML or use something like:
The reverse is optional at the end, it accepts a boolean and if it's true, it will reverse the Array for you, very handy way to reverse your Array...
If you wants to sort on mulitple fields inside controller use this
See also https://docs.angularjs.org/api/ng/filter/orderBy
Sorting can be done by using 'orderBy' filter in angular.
Two ways: 1. From view 2. From controller
Syntax:
For example:
Syntax:
For example:
Created Pipe for sorting. Accepts both string and array of strings, sorting by multiple values. Works for Angular (not AngularJS). Supports both sort for string and numbers.
I wrote this handy piece to sort by multiple columns / properties of an object. With each successive column click, the code stores the last column clicked and adds it to a growing list of clicked column string names, placing them in an array called sortArray. The built-in Angular "orderBy" filter simply reads the sortArray list and orders the columns by the order of column names stored there. So the last clicked column name becomes the primary ordered filter, the previous one clicked the next in precedence, etc. The reverse order affects all columns order at once and toggles ascending/descending for the complete array list set:
User array instead of multiple orderBY