Ordering Filter
app.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});
Data of JSON:
{
"results": {
"100": {
"numbers": {
"1": {
"key": "value"
},
"2": {
"key": "value"
},
"3": {
"key": "value"
}
}
},
"200": {
"numbers": {
"1": {
"key": "value"
},
"2": {
"key": "value"
},
"3": {
"key": "value"
}
}
}
}
}
Show on screen:
<div ng-repeat="(k,v) in results">
<span ng-repeat="(k1,v1) in v | orderObjectBy:k1:true">{{k1}}</span>
</div>
The issue with orderObjectBy:k1:true
is that it returns an array of keys rather than the values. To rectify this and reverse the order, we need to make adjustments. How can we accomplish this?