javascript - sort array of objects by property in object -
i have array of objects like:
[ {date: "2016-01-07t15:01:51+00:00", text: "lorem ipsum"}, {date: "2016-22-08t15:04:36+00:00", text: "dolor"}, // etc. ]
how's best way sort these date property? i'm mapping array react component, solution works within map function guess preferred, not essential.
i'm trying use sort()
method @ moment, can't work how feed date property.
you can have custom sort function:
var data = [{ date: "2016-07-01t15:01:51+00:00", text: "lorem ipsum" }, { date: "2016-02-22t15:04:36+00:00", text: "dolor" }, { date: "2015-08-22t15:04:36+00:00", text: "test" }] var result = data.sort(function(a, b) { var date1 = new date(a.date); var date2 = new date(b.date); console.log(date1, date2); return (+date1 - +date2); }); console.log(result)
Comments
Post a Comment