xxxxxxxxxx
var l = [new Date(2022, 10, 10), new Date(2022, 10, 8), new Date(2022, 10, 6), new Date(2022, 10, 15), new Date(2022, 10, 2)];
console.log(l.sort((date1, date2)=>date1-date2));
xxxxxxxxxx
var array = [{id: 1, date:'Mar 12 2012 10:00:00 AM'}, {id: 2, date:'Mar 8 2012 08:00:00 AM'}];
array.sort(function(a, b) {
var c = new Date(a.date);
var d = new Date(b.date);
return c-d;
});
xxxxxxxxxx
array.sort((a, b) => b.date.localCompare(a.date));
// source: Dave Gray, Youtube
xxxxxxxxxx
function sortFunction(a,b){ var dateA = new Date(a.date).getTime(); var dateB = new Date(b.date).getTime(); return dateA > dateB ? 1 : -1; }; var array = [{id: 2, date: "Mar 28 2012 08:00:00 AM"}, {id: 1, date: "Mar 12 2012 10:00:00 AM"}]; array.sort(sortFunction);
xxxxxxxxxx
array.sort(function(o1,o2){
if (sort_o1_before_o2) return -1;
else if(sort_o1_after_o2) return 1;
else return 0;
});
xxxxxxxxxx
var compareDate = (t1: Interface, t2: Interface) => {
var t1Date = new Date(t1.createDate);
var t2Date = new Date(t2.createDate);
return t1Date > t2Date ? -1 : 1;
};
arr = [objectarr].sort(compareDate);