0

I have this JSON Object:

 {
        "content":
        {
            "data":
            {
                "totalCost":244112,
                    "currency":"USD",
"summary": [
                {
                    "category": "NewCars",
                    "count": 2,
                    "cost": 91233,
                    "details": [
                        {
                            "name": "Ferrari",
                            "Model": "Enzo",
                            "condtion": "Excellent",
                            "kms": 10000,
                            "Year": 2009,
                            "cost": 69864
                        },
                        {
                            "name": "RollsRoyce",
                            "Model": "Ghost",
                            "condtion": "OK",
                            "kms": 10000,
                            "Year": 2006,
                            "cost": 21369
                        }
                    ]
                },
                {
                    "category": "UsedCars",
                    "count": 2,
                    "cost": 146464,
                    "details": [
                        {
                            "name": "Buggati",
                            "Model": "Veyron",
                            "condtion": "Excellent",
                            "kms": 10000,
                            "actionYear": 2011,
                            "cost": 85500
                        },
                        {
                            "name": "Lamborgini",
                            "Model": "Aventador",
                            "condtion": "OK",
                            "kms": 10000,
                            "Year": 2010,
                            "cost": 60964
                        }
                    ]
                },
                {
                    "category": "TwoWheelers",
                    "count": 1,
                    "cost": 6415,
                    "details": [
                        {
                            "name": "Suzuki",
                            "Model": "Hayabusa",
                            "condtion": "Bad",
                            "kms": 10000,
                            "Year":2009,
                            "cost": 6415
                        }
                    ]
                }
            ]
            }
        ,"metaData":
            {
                "title":"Details"
            }
        }
    ,"status":200
    }

Is there a way to sort the above JSON on the inner keys like name, model, condition, kms, year and cost in Javascript

Thanks in advance. Any help would be appreciated.

P.S. New to JSON and scripts

Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • yes you can, arrays have the sort property. But i dont think your json is valid. Try to validate it here: [validator](http://json.parser.online.fr/) – Ibu Apr 15 '14 at 21:24
  • Now it is.. I have added the necessary change but do you have nay example where this kind of JSON was sorted?? – Nick Div Apr 15 '14 at 21:32
  • @Ibu Or a code snippet or something – Nick Div Apr 15 '14 at 21:32
  • 1
    Look at this question: http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects as long as you can get access the details as an array you can sort it. – vivekian2 Apr 15 '14 at 21:33
  • @VivekAseeja : I'll se what I can get from it. Thanks – Nick Div Apr 15 '14 at 21:37
  • You mean you want to sort each of the `details` arrays? – Bergi Apr 15 '14 at 22:33
  • @Bergi Yes.. as you can see there are three details arrays in the JSON I gave, so I want to sort each of them with the choice of my key – Nick Div Apr 16 '14 at 01:54

2 Answers2

0

You can just invoke sort function (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) on the array. If it is an inner array of the JSON object, just call the sort on that inner key. The sort function does in place sorting, so even if the array is internal, it will get sorted.

For you example JSON, assuming data variable holds the JSON, following are valid sort invocations.

data.content.data.summary.sort(function(a, b){
   //add comparator logic here to define ordering between 2 elements
});

or

data.content.data.summary[0].details.sort(function(a, b){
   //add comparator logic here to define ordering between 2 elements
});
0

Assuming you have parse the JSON already like

var obj = JSON.parse(jsonString);

then you just need to iterate the object so that you can call the sort method on each of the arrays, comparing their items by the desired property:

var propertyname = "name"; // or "Year" or whatever

var summary = obj.content.data.summary;
for (var i=0; i<summary.length; i++)
    summary[i].details.sort(function(a, b) {
        a = a[propertyname]; b = b[propertyname];
        return +(a>b)||-(b>a);
    });
Bergi
  • 630,263
  • 148
  • 957
  • 1,375