How to sort grouped tasks by with a field "priority" that is defined in page?

What I’m trying to do

I’d like to adapt this dataview task query to sort group of tasks by a field value. Field can be called e.g., “priority”, and it’s defined in a page, like this: “priority::10”

This is what I have now, groups are sorted by origin.file.mtime currently, but it’s not sufficient.

TASK FROM "" WHERE contains(text, "#task") AND !completed GROUP BY as origin SORT origin.file.mtime DESC

Things I have tried

I’ve searched many times, but failed to find this scenario. And I cannot figure out the way how to access in this query from sort origin.file the field value.
Thank you for your help!

We need to see some examples of your task, both to be able to reproduce your issue, and to help you understand what is going on.

But if you’re grouping by then that could hinder sorting outside of that group. And when you’re sorting on the modification time, it doesn’t sort on anything else…

Finally, if the priority is given for the entire file, that might also affect stuff.

So please provide some more information, to enable some of us to possibly help you any further.

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 for code blocks) is properly shown.

I’d like to replace sorting based on the modification time in the given example with the sorting based on the field in the file.

The group by is there to provide a context to what the group of tasks relate to. If is a complication to the sorting, I can adapt to other kind of grouping.

Yes, the priority is given for the file.

Here is the example:

There are 2 files:
File A has priority in frontmatter described as priority::5 and it has couple of tasks.
File B has priority in frontmatter described as priority::3 and it also has couple of tasks.

These tasks contains text #task, so that I can filter only relevant tasks from other [ ] which are not “tasks”.

In daily note I’d like to query tasks (which have text #task) from all files, group them based on that file (to see the context) and sort them based on the priority field in frontmatter defined in the files.

One variant which seems to do what you ask for, if I understood it correctly is:

WHERE file.folder = this.file.folder
WHERE contains(text, "#task") AND !completed
GROUP BY priority + ": " + as origin
SORT origin

This produces output like the following:

Note that I’ve used WHERE file.folder = this.file.folder to limit the test set, feel free to replace with FROM "" or adapt to your situation.

The main trick is that I included the priority into the GROUP BY clause, so that the priority is taken into consideration when sorting. This assumes that you want all the task groups sorted according to the file and priority.

If you want just the task of any given group sorted, you add a sort clause before the GROUP BY clause.

1 Like

This is brilliant! Thank you very much, it’s exactly what I was looking for! :slight_smile:

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