Dataview SORT multiple fields with multiple null values

What I’m trying to do

I have a standard task template with a bunch of relevant inline fields:

  • EXAMPLE TASK 1 || [duedate:: ] || [priority:: ] || [person:: ] || [project:: ] || [topic:: ] || [type:: protean] || [tags::] || [date:: {{date:YYYY-MM-DD}}] || [state:: valid] ||

As the template is standard, it means that for a lot of tasks i am left with a bunch of null values.

I SORT by duedate and priority, both of which are ASC sorts, which means the null values always sort to the top and i can’t sort DESC.

I managed to find a thread that showed how to sort by field if value is present:
SORT default(priority, “”) ASC

which works great, but as soon as i try to put in a second sort field using the same logic, it seems to trip up:
SORT default(priority, “”) ASC, default(duedate,date(“”)) ASC

Thanks in advance.

Here is a test note with a query which you can play around with:

- [ ] task 0
- [ ] task 1  [priority:: 1]
- [ ] task 2  [duedate:: 2024-02-02] [priority:: 1]
- [ ] task 3  [duedate:: 2024-03-03] [priority:: 3]
- [ ] task 4  [duedate:: 2024-03-03] 
- [ ] task 5  [duedate:: 2024-05-05] [priority:: 5]


```dataview
TABLE WITHOUT ID pureText, item.priority, item.duedate
WHERE file = this.file
FLATTEN file.tasks as item 
FLATTEN regexreplace(item.text, "\[[^\]]*\]", "") as pureText
SORT default(item.priority, "") ASC, default(item.duedate, date("9999-12-31")) ASC
```

The trick is that you need to provide a suitable date to place it in the correct order for your query to work. In the example above I’ve used the date 9999-12-31, which most likely won’t occur in your natural dates, and comes at the end. If you want to sort the other way around, you should change the year around, i.e. to 1970-01-01 which is the lower value for a date using epoch times.

Similar for the priority field, if you’d like for it to appear in either end of the scale, you could vary between “” (or a larger number) and -1 (or a lower number) to make it appear it into the list at a proper place according to your preferences.

Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like in code blocks or queries) is properly shown.

1 Like

Thanks for all the good info.

The only problem with the FLATTEN and regexreplace commands are that they loose the original task as a result. i.e. I can no longer go through and tick off the various tasks (and mark them with a done date variable), I can only view them in a table.

Any other thoughts?

Actually in a task query you can change the task text through a FLATTEN ... as visual, and keep the linking. So all I’ve written can also be done using a task query. I used a table query when testing in order to easier show extra information. If you’re not able to transform it, let me know later on and I can see if I can type it out.

1 Like

thanks Holroy, huge help.

Is there a way to list an entire point in a dataview search?

i.e.

- test [tag:: test]
	- test line 1
		- test line 2
		- [ ] test task 1

I want a dataview that shows all items (and the subsequent items) that have “[tag:: test]” mentioned?

In this example, it would return exactly the information i have showed, but may also include other bullets including [tag:: test]?

If doing a TASK query you’ll normally only target the current line, although you’ve got some access to its children for some extra matching. It’s a lot harder to target them (especially on lower levels). So I’d recommend a different style of tagging to make it easier on yourself to query it.

Normally when presenting the results, a TASK query will also display its children. In a LIST query that’s not so normal, and within a TABLE query on list/task items you can potentially use the children field of the item in question.


I saw that your other post just closed down, so here are some brief points on some stuff to try related to formatting inline fields:

.inline-field-key[data-dv-key="hiddenKey"],
.inline-field-key[data-dv-key="grade"],
.inline-field-key[data-dv-key="summary"],
.inline-field-key[data-dv-key="cost"] {
  display: none;
}

body .inline-field-key[data-dv-key="visible"] {
  background-color: grey;
  color: yellow; 

  & + .inline-field-value span {
    color: blue;
   }
}

.inline-field-key[data-dv-key="cost"] + .inline-field-value::after {
  content: " $";
}

.inline-field-key[data-dv-key="grade"] + .inline-field-value {
  background-color: hsl(280, 68%, 15%);
  border-radius: 3px;
  padding: 1px;
}

.inline-field-key[data-dv-key="grade"] + .inline-field-value::before {
  content: " ◊ ";
}

.inline-field-key[data-dv-key="summary"] + .inline-field-value {
  display: block;
  margin-left: 10% !important;
  margin-right: 10% !important;
  background-color: hsl(180, 100%, 8%) !important;
  border-radius: 10px;
  padding: 6px 15px;
  color: var(--text-normal) !important;
}

If you toss the code above into a CSS snippet, and enable it, and have a note like the following:

[hiddenKey:: hidden value?] and [visible:: visible value].

Field with `::after`:  [cost:: 123]

A graded field: [grade:: 3]

[summary:: A summary paragraph, which can be rather long if you want it to]

It should display something like this:

image

Here I showcase both how to hide the key (but it’s needed to be able to target the value part) when using square brackets for inline field definition. And some alternatives related to pre- or postfixing text, and various styling variants based upon some bits and pieces I’m using in my own vault.

1 Like

Thanks again holroy, it’s all really helpful.

Just on the point about trying to filter via lists, it may be useful to understand what i’m trying to achieve.

It’s all about ease of use - the lowest required effort for the highest data effectiveness.

I religiously use my daily notes and creating new notes (for example meeting minutes) and linking or even navigating around to edit other notes (day to day) is wasted time when i want to be able to dump anything in a bullet (or other) within my daily notes, mark it with some variables and then i’ll have a summary dataview of the information somewhere else.

The best way i know to do this today is with Tasks, they seem to be very flexible and easy to filter, but i cant mark everything as a task that isn’t. I am struggling a bit with lists or tables, but ultimately i want to use them similarly to tasks…

Appreciate all your responses, thanks again.

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