How to sort a dataview list by the existence of a value

Things I have tried

I’ve scoured the information page and other posts on here and I haven’t been able to figure out how to do this simple thing.

Here’s my current query (000 Domus is my home note, ignore that):

FROM #project/active
WHERE != "000 Domus"
SORT due asc

What I’m trying to do

I am trying to use a dataview query to generate a list of active projects, sorted by due date, with projects with no due date at the bottom and projects with the closest due date at the top. But when I sort by due ASC, it places files with no due date above files with the closest due date. I can’t find information on how to sort by whether there is a “due” object in the YAML frontmatter.

Try this trick:
SORT default(due, "") ASC

That did it! Thanks! Now I just need to figure out why it works :thinking:

To check how things work, sometimes is useful create a dataview table for tests purposes. For example, use this:

TABLE due, default(due, "") AS Test, default(due, date(2022-02-31)) AS "Test with date",  default(due, "string") AS "Test with string"
FROM #project/active
WHERE != "000 Domus"
SORT default(due, "") ASC

The function default(field, value) do this: «If field is null, return value ; otherwise return field».

With the expression SORT default(due, "") ASC we do this: if due is null, return nothing (""). You can use other things: strings, etc… except dates (well, you can use dates, but then they are sorted in relation with there real dates).

If you play with the query/table above you will understand what you control as output to null values with the function default().

This is crazy helpful. I’ve implemented lots of DQL from different sources without ever really understanding what I was doing or why it was working (which it often didn’t), so having a way to test functions and see what they do will be great. Thanks again!

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