Still new to this. I encountered a problem when trying to sort an array of objects with a Date type. For example: I have 3 cars with: (Name, Date of first registration(type Date)). How do I sort this array? Should I change date to string? Thanks.
function car(Make, Model, Date) {
return Make + Model + Date;
}
var date1 = '2010';
var date2 = '2003';
var date3 = '2015';
var d1 = new Date(date1);
var d2 = new Date(date2);
var d3 = new Date(date3);
var n1 = d1.getFullYear();
var n2 = d2.getFullYear();
var n3 = d3.getFullYear();
var x = car('Mercedes', ' c220 ', n1);
var y = car(' Toyota', ' Prius ', n2);
var z = car(' Audi', ' a3 ', n3);
var polje = [x, y, z];
console.log(polje);
.as-console-wrapper { top: 0 }
new Date('2010').getFullYear()is identical to+2010. ;-)