Vaibhav Vaibhav - 5 months ago 28
Javascript Question

OrderBy in Angular.js for (key,value) object in reverse based on keys

I want to display the following results in the reverse order using javascript / Angular.js. This is an associative array with keys as Year(Numerical) and value as array of objects of months of those year.

var obj = { '1991': [{'year':1991,'month':'Jan'},{'year':1991,'month':'Feb'}],
'1992': [{'year':1992,'month':'March'},{'year':1992,'month':'Dec'}],
'2000': [{'year':2000,'month':'Jan'}]

SO when this basically shows data on html using
ng-repeat="(key,value) in obj"
it shows 1991 as first and 2000 as last. I just want to reverse my order of display showing 2000 as first and 1991 as last, using minimalist Complexity on Front-end i.e O(n). Applying orderBy is not helping me. I am not sure if i need a custom filter for this. Any leads would be appreciated as I dont want to change the JSON structure. It should be the same.



Get the objects keys and then use the keys to get the value from the object.


<div ng-repeat="key in keys">


$scope.obj = {
  '1991': [{
    'year': 1991,
    'month': 'Jan'
  }, {
    'year': 1991,
    'month': 'Feb'
  '1992': [{
    'year': 1992,
    'month': 'March'
  }, {
    'year': 1992,
    'month': 'Dec'
  '2000': [{
    'year': 2000,
    'month': 'Jan'

// get object keys and sort them.
$scope.keys = Object.keys($scope.obj).reverse();