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
  • 4,904
  • 9
  • 60
  • 116

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
  • 572,313
  • 128
  • 898
  • 1,281