Hello guys I want to sort array by " New_date " but in some cases there is another object inside of main object with key " New_date ". Do you know how could I sort array in this case?
arr.sort(function(a, b) {
a = new Date(a.New_date);
b = new Date(b.New_date);
return a>b ? -1 : a<b ? 1 : 0;
});
arr=
[0: {name:"Angel", data "random data", New_date: "2020-12-10"},
1: [0: {name:"Tom", data "random data"},
1: {name:"Tom", data "random data"},
New_date:"2020-11-10"],
2:name:"Angel", data "random data", New_date: "2020-09-10"}]
Here is a solution with a correctly formatted array.
let arr = [ { name: "Angel", data: "random data", New_date: "2020-12-10" }, { name: "Tom", data: "random data", New_date: "2019-11-10" }, { name: "Tom", data: "random data", New_date: "2020-11-10" }, { name: "Angel", data: "random data", New_date: "2020-09-10" }, ] console.log('ascending', arr.sort((a, b) => new Date(a.New_date) - new Date(b.New_date))) console.log('descending', arr.sort((a, b) => new Date(b.New_date) - new Date(a.New_date)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.