I have an array in this format. I need to sort it descending:
[{
"9-Sep" : 6
}, {
"8-Sep" : 11
}, {
"7-Sep" : 4
}, {
"6-Sep" : 11
}, {
"5-Sep" : 16
}, {
"4-Sep" : 14
}, {
"3-Sep" : 3
}, {
"2-Sep" : 11
}, {
"15-Sep" : 28
}, {
"14-Sep" : 6
}, {
"13-Sep" : 8
}, {
"12-Sep" : 15
}, {
"11-Sep" : 24
}, {
"10-Sep" : 19
}];
I am using this function but it is sorting only if values are not there.
function myname() {
var ad = new Date(),
bd = new Date(),
months = {
Jan: 0, Feb: 1, Mar: 2, Apr: 3, May: 4, Jun: 5,
Jul: 6, Aug: 7, Sep: 8, Oct: 9, Nov:10, Dec:12
};
json.sort(function (a,b) {
var as = a.split('-'),
bs = b.split('-');
ad.setDate(as[0]);
ad.setMonth(months[as[1]]);
bd.setDate(bs[0]);
bd.setMonth(months[bs[1]]);
return ad - bd;
});
};
How to sort the above array with values?
["9-Sep", "8-Sep", ...]) but not an array of objects ([{"9-Sep" : 6}, {"8-Sep" : 11}, ...]). If you have an array of objects, thenaandbwill be objects and you'd have to extract the property name first. It might be easier to use an array of arrays instead:[["9-Sep", 6], ["8-Sep", 11], ...].