Within the users array are objects containing personal information of various individuals. The getDatesfromString function is used to convert a string to date format, and the sorted_user variable is intended to store the sorted user array. My question lies in understanding how the sort function arranges the users based on their Date of Birth. I would appreciate a detailed explanation.
let users = [
{
firstName: "John",
lastName: "wick",
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b8bdbabca5bbb1b992b5b3bfbbbefcb1bdbf">[email protected]</a>",
DOB:"22-01-1990",
},
{
firstName: "John",
lastName: "smith",
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f9fcfbfde0fefae7fbd3f4f2fefaffbdf0fcfe">[email protected]</a>",
DOB:"21-07-1983",
},
{
firstName: "Joyal",
lastName: "white",
email:"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3898c9a828f948b8a9786a384828e8a8fcd808c8e">[email protected]</a>",
DOB:"21-03-1989",
},
];
function getDateFromString(strDate) {
let [dd,mm,yyyy] = strDate.split('-')
return new Date(yyyy+"/"+mm+"/"+dd);
}
// console.log(sorted_users);
let sorted_users=users.sort(function(a, b) {
let d1 = getDateFromString(a.DOB);
let d2 = getDateFromString(b.DOB);
return d1-d2;
});
console.log(sorted_users);