Sorting date property in DataviewJS

What I’m trying to do

I would like to find a solution to sorting a dataviewjs table that uses Metadata Menu to generate a list of project notes. When I sort by deadline (which is a date property in all of my project notes), the notes without a date are listed at the top. I would like them to be at the bottom (after a list of notes sorted by deadline asc).

Here is my query:

const {fieldModifier: f} = this.app.plugins.plugins["metadata-menu"].api;

dv.table(["project", "today?", "deadline"],
dv.pages('')
	.where(p => p.scheduled == false)
	.where(p => p.priority == "⏫️")
	.sort(k => [k.deadline, 'asc'])
	.map(p => [
		p.file.link, 
		f(dv, p, "scheduled"), 
		f(dv, p, "deadline")
	])
)
// change column width
this.container.querySelectorAll(".table-view-table td").forEach(s => s.style.width ="300px");

Things I have tried

I found a solution for regular dataview on this thread (Sorting so the date works in Dataviews), but would like to have a solution for dataviewjs.

Try adding a second sort line where you sort on the date being present or not. Or possible some other sort order to be used with no dates.

I always get the order mixed up when not having data to test with , but do something like:

 dv.pages(...)
   .where( ... )
   .sort(k => !k.deadline, 'desc')
   .sort(k => k.deadline, 'asc')
   .map( ... )

And try switching then around of it doesn’t work at first… :slight_smile:

Thank you! When I added brackets (and put it in this order) it worked:
.sort(k => [k.deadline, ‘asc’])
.sort(k => [!k.deadline, ‘desc’])

1 Like

I’m not sure why the brackets are needed in your case, but if it works it works! :smiley:

1 Like