Trouble sorting pages by one of their fields in DataviewJS

What I’m trying to do

I have a dataviewJS custom script that fetches calendar events and displays them in my daily note as a list:

class calendar {
	createLists(category, targetDate, dv) {
		let pages = dv.pages().where(p => p.file.path.startsWith(`journal/full-calendar/${category}/${targetDate}`));
		let allDay = pages.filter(p => p.allDay === true).map(p => `<a href="obsidian://open?vault=BenjVault&file=${p.file.path}">•</a> ${p.title}`).join("  \n");
		let events = pages.filter(p => p.allDay === false) .sort(p => p.startTime) .map(p => `<a href="obsidian://open?vault=BenjVault&file=${p.file.path}">•</a> ${p.startTime}-${p.endTime} ${p.title}`).join("  \n");
	
		let allDayString = "";
		let eventsString = "";

		if (allDay) {
			allDayString = `<span class='cal${category.charAt(0).toUpperCase() + category.slice(1)}'>` + allDay + "</span>";
		}

		if (events) {
			eventsString = `<span class='cal${category.charAt(0).toUpperCase() + category.slice(1)}'>` + events + "</span>";
		}

		return { allDayString, eventsString };
	}


	displayCalendars(dv, targetDate) {
	
		let contentAdded = false;
		let allDayEvents = [];
		let timedEvents = [];
		
		['personnel', 'radio', 'autres', 'sorties', 'bnb'].forEach(category => {
	  		const { allDayString, eventsString } = this.createLists(category, targetDate, dv);
	  		if (allDayString) allDayEvents.push(allDayString);
	  		if (eventsString) timedEvents.push(eventsString);
		});
	
		if (allDayEvents.length) {
		  dv.paragraph(allDayEvents.join("<br>"));
		  contentAdded = true;
		}
	
		if (timedEvents.length) {
		  dv.paragraph(timedEvents.join("<br>"));
		  contentAdded = true;
		}
	
		if (!contentAdded) {
		  dv.paragraph("Rien dans le calendrier.");
		}

	}
	
	wrapWithCategoryClass(content, category) {
	  return `<div class='cal${category.charAt(0).toUpperCase() + category.slice(1)}'>` + content + "</div>";
	}	
	

These calendar events are proper notes, in the format wanted by the Full-Calendar plugin, so they contain a startTime YAML field with values such as “9:30” or “15:00”.

But, for some reason, the sort() method doesn’t work anymore. (I’m pretty sure it worked correctly until recently.) As an example here is the output of this code in today’s daily note:

• 11:30-12:30 Event 1
• 15:00-17:00 Event 2
• 09:30-11:00 Event 3

Things I have tried

I tried await dv.pages() to see if the issue was because the array was not entirely populated before the sort method was called, but that didn’t change anything.

Any ideas? I’m not sure what I should try next.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.