[
0: {date: "02-04-2020", count: 1}
1: {date: "16-04-2020", count: 2}
2: {date: "10-04-2020", count: 1}
3: {date: "15-04-2020", count: 4}
4: {date: "04-04-2020", count: 4}
]
From the above array of objects I need to sort them according to date and get the output as follows
[
0: {date: "02-04-2020", count: 1}
1: {date: "04-04-2020", count: 4}
2: {date: "10-04-2020", count: 1}
3: {date: "15-04-2020", count: 4}
4: {date: "16-04-2020", count: 2}
]
I tried using the following method
const sorted_array = data
.slice()
.sort(
(a, b) => new Date(a.date).valueOf() - new Date(b.date).valueOf()
);
I used .slice before sort since I get the error Cannot assign to read only property '0' of object '[object Array]
so I referred Error while sorting array of objects Cannot assign to read only property '2' of object '[object Array]' and added slice.
But the sorting is not done correctly, I need some help in fixing this. Thank you.