redcoder
redcoder

Reputation: 2333

sort json properties in descending order

I have the following json, how can I sort the "c" value in DESC order?

[{"id":"2","val":"2pm.com","c":"4"}, 
{"id":"2","val":"Adidas","c":"2"},
{"id":"2","val":"AJ Morgan","c":"2"},
{"id":"2","val":"Alfio Raldo","c":"3"},
{"id":"1","val":"ASOS","c":"112"},
{"id":"2","val":"ASOS","c":"34"},
{"id":"2","val":"ASOS Curve","c":"39"},
{"id":"2","val":"Baby Be Dazzled","c":"13"}]

I use PHP and ajax

Upvotes: 0

Views: 99

Answers (1)

Deepak Ingole
Deepak Ingole

Reputation: 15762

You can do something in JS like,

arr.sort(compare);

function compare(obj1, obj2) {
    return obj1.c - obj2.c;
}

console.log(arr);

Fiddle

Upvotes: 1

Related Questions