javascript - Sorting json object by value -
i have json object below, how sort using date?
json = {"date_hash":{"second_bleed":"2014-09-08","sixth_boost":"2014-10-28","first_boost":"2014-06-24","first_bleed":"2014-08-08","fifth_boost":"2014-09-30","fourth_bleed":"2014-11-03","second_boost":"2014-07-15","fourth_boost":"2014-09-02","third_bleed":"2014-10-06","primary_injection":"2014-06-02","third_boost":"2014-08-05"}}
i tried doing
json['date_hash'].sort(function(a, b){ });
sort not function json?
the date_hash object json object doesn't have order (not sortable).
you should try use array:
{"date_hash": [ {"name": "second_bleed", "date": "2014-09-08"}, .... {"name": "sixth_boost", "date": "2014-09-28"} ]}
and similar function you're using sort.
function (a, b) { if (a.date < b.date) { return -1; } else if (a.date > b.date) { return 1; }; return 0; }
Comments
Post a Comment