1

I have array of json object as following

{
    "01/15/2015_1": [
        [
            {
                "sequenceType": -1,
                "delflag": -1
            },
            {
                "sequenceType": -1,
                "delflag": -1
            }
        ]
    ],
    "01/15/2015_2": [
        [
            {
                "sequenceType": -1,
                "delflag": -1
            },
            {
                "sequenceType": -1,
                "delflag": -1
            }
        ]
    ]
}

By iterating it with jquery's each() I am getting object as following order:

1."01/15/2015_1"
2."01/15/2015_2"

But I want it in reverse() say as following:

1."01/15/2015_2"
2."01/15/2015_1"

Need help..

Is it possible with ng-repeat?

Mahesh
  • 964
  • 1
  • 10
  • 27
  • possible duplicate of [jQuery: Sort results of $.each](http://stackoverflow.com/questions/8886494/jquery-sort-results-of-each) – Ole Viaud-Murat Jan 15 '15 at 13:39
  • 2
    Actually you are iterating properties of the object, not array. ECMAscript does not actually define order for the properties. http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – ikettu Jan 15 '15 at 13:45

2 Answers2

7

An Objekt has no defined order, so you cannot make sure this works well in every browser.

You have to grab the keys, Object.keys(), sort them and loop through it by key.

e.g.:

var obj = {
    "01/15/2015_1": [
       "example1"
    ],
    "01/15/2015_3": [
        "example3"
    ],
    "01/15/2015_2": [
        "example2"
    ]
}
var keys = Object.keys( obj ).sort();
var klen = keys.length;
for( var idx = 0; idx < klen; idx++ ){
    console.log( obj[ keys[ idx ] ] )
}
mpneuried
  • 181
  • 3
0

Probably duplicate of https://stackoverflow.com/questions/5906061/can-i-make-jquerys-each-method-iterate-in-reverse:

Sulution:

$.each(array.reverse(), function() { ... });
Community
  • 1
  • 1
Beri
  • 10,277
  • 3
  • 24
  • 47