1array = [{
2 id: 1, name: "test1", date: "2020-01-05",
3 id: 2, name: "test2", date: "2020-01-02"
4 }]
5
6array.sort(function (a, b) {
7 var dateA = new Date(a.date), dateB = new Date(b.date)
8 return dateA - dateB
9});
10
11console.log(array) //array is now sorted by date
1const activities = [
2 { title: 'Hiking', date: new Date('2019-06-28') },
3 { title: 'Shopping', date: new Date('2019-06-10') },
4 { title: 'Trekking', date: new Date('2019-06-22') }
5]
6
1const sortedActivities = activities.slice().sort((a, b) => b.date - a.date)
2
1array.sort(function(o1,o2){
2 if (sort_o1_before_o2) return -1;
3 else if(sort_o1_after_o2) return 1;
4 else return 0;
5});