I have the following dataviewjs code that gets birthdates from ‘people’ files and then is supposed to sort them in order of the number of days until their next birthday. It works great, showing the persons name (name of file), Birthday, Days Until Next Birthday, and Age, but it refuses to sort by ‘Days Until Next Birthday’, instead it sorts alphabetically by Name. I disabled all my plugins and snippets thinking something was influencing it, but even with just Dataview enabled, it won’t sort. I’ve been using CoPilot to write the code, and we tried a dozen different ways, including moving the days left column to be first with no success. Any ideas?
```dataviewjs
const pages = dv.pages('"CRM/People"')
.where(p => p.fileClass == "People" && p.dates && p.dates.length > 0)
.filter(p => p.dates.some(date => date.includes("Birthday")));
const today = new Date();
const result = pages.map(p => {
const birthdays = p.dates
.filter(date => date.includes("Birthday"))
.map(date => new Date(date.split("|")[0].trim()));
const calculateDaysUntilNextBirthday = (birthday) => {
const nextBirthday = new Date(today.getFullYear(), birthday.getMonth(), birthday.getDate());
if (nextBirthday < today) {
nextBirthday.setFullYear(today.getFullYear() + 1);
}
return Math.ceil((nextBirthday - today) / (1000 * 60 * 60 * 24));
};
const nextBirthday = birthdays.reduce((closest, date) => {
const daysUntil = calculateDaysUntilNextBirthday(date);
return daysUntil < calculateDaysUntilNextBirthday(closest) ? date : closest;
}, birthdays[0]);
const daysUntilNextBirthday = calculateDaysUntilNextBirthday(nextBirthday);
const age = today.getFullYear() - nextBirthday.getFullYear() - (today < new Date(today.getFullYear(), nextBirthday.getMonth(), nextBirthday.getDate()) ? 1 : 0);
return {
name: "[[" + p.file.name + "]]",
birthday: nextBirthday.toLocaleDateString(),
daysUntilNextBirthday: daysUntilNextBirthday,
age: age
};
}).filter(p => p.birthday);
console.log(result);
const sortedResult = result.sort((a, b) => a.daysUntilNextBirthday - b.daysUntilNextBirthday);
const formattedResult = sortedResult.map(p => [
p.name,
p.birthday,
p.daysUntilNextBirthday,
p.age
]);
dv.table(["Name", "Birthday", "Days Until Next Birthday", "Age"], formattedResult);
```