0

I'm trying to sort the json below, but until now couldn't find the way. I get this json by jQuery ajax with datatype json. How can I apply sort to this keeping the index? Every time i try to sort javascript throws an error telling undefined is not a function. I already know that json or objects don't have sort.

So how can I achieve this? I really need to keep this index.

{
    "header": {
        "user": "1059",
        "authenticated": true,
        "isGuest": false,
        "time": 1416362117
    },
    "batches": {
        "3503": {  // Preserve this index
            "user": "1059",
            "auction": "1826",
            "number": "1",
            "published": "1",
            "type": "1",
            "status": "1",
            "date_start": "1415379600",
            "date_end": "1417021200",  // Sort by this value
        },
        "3583": {
            "user": "1059",
            "auction": "1886",
            "auto_value": "0.00",
            "number": "1",
            "published": "1",
            "type": "1",
            "status": "1",
            "date_start": "1415376000",
            "date_end": "1417017600",
        },
    }
}
  • http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – Cheery Nov 19 '14 at 02:22
  • Just to add the main idea of the link above `JavaScript objects are unordered by definition (see the ECMAScript Language Specification, section 8.6). The language specification doesn't even guarantee that, if you iterate over the properties of an object twice in succession, they'll come out in the same order the second time` – Cheery Nov 19 '14 at 02:32
  • Sorry @Cheery that answer doesn't help me. – thiagovidal Nov 19 '14 at 02:34
  • I cited the main part - you can not sort object's properties as they do not have initial order. Create an array, where property and it's value will be stored together (or simple objects in array) - it is possible to sort the array only. – Cheery Nov 19 '14 at 02:34
  • check this: http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects – AAhad Nov 19 '14 at 02:46
  • how to sort an array of json objects by property: http://www.levihackwith.com/code-snippet-how-to-sort-an-array-of-json-objects-by-property/ – AAhad Nov 19 '14 at 02:49

1 Answers1

0

As I wrote and cited in comments - properties do not have an order. Make an array of single-property objects, which you can sort. Something like this:

var data = {
    "header": {
        "user": "1059",
        "authenticated": true,
        "isGuest": false,
        "time": 1416362117
    },
    "batches": [ // this is an array of objects, not a single object
        {"3503": {  // Preserve this index
            "user": "1059",
            "auction": "1826",
            "number": "1",
            "published": "1",
            "type": "1",
            "status": "1",
            "date_start": "1415379600",
            "date_end": "1417021200",  // Sort by this value
        }},
        {"3583": {
            "user": "1059",
            "auction": "1886",
            "auto_value": "0.00",
            "number": "1",
            "published": "1",
            "type": "1",
            "status": "1",
            "date_start": "1415376000",
            "date_end": "1417017600",
        }}
    ]  // end of array
}

data.batches.sort(function(a, b) {
    return a[Object.keys(a)[0]].date_end - b[Object.keys(b)[0]].date_end;
});

console.log(data.batches);

And you can keep those numbers inside of the object containing user, auction and so on for easier access.

Cheery
  • 16,063
  • 42
  • 57