Sort by tag

What I’m trying to do

I am trying to sort a list of tasks based on the value of the tag in the task.

Things I have tried

I have the following tasks:

- [ ] #Y2023/Q3/SP5/P11 - Test 1
- [ ] #Y2023/Q3/SP5/P21 - Test 3
- [ ] #Y2023/Q3/SP5/P12 - Test 2
- [ ] #Y2023/Q3/SP5/P6 - Test 6
- [ ] #Y2023/Q3/SP5/P21 - Test 4

I am running a query to list all tasks that have a tag that has the following properties

  • The tag contains the text #Y2023
  • The tag contains the text SP
  • The tag also contains the text /P

The following query searches all notes and returns the results i need:

TASK 
FROM ""
WHERE (contains(tags, "Y2023") 
AND contains(tags, "SP")
AND contains(tags, "/P"))  and !completed
SORT tag

The output i get is shown below:

Two questions:

  • Why does the SORT tag statement not work? I would expect task 3 to be 2nd on the list and task 5 to be higher up on the list as well. (Note - each of these tasks could be in different daily notes)

  • What is the difference between the SORT tag option and the sort by tag option described here - Sorting - Tasks User Guide - Obsidian Publish

Thanks in advance

Hi

You must specify the sort order: asc (ascending) and desc (descending):

And now, my method for sorting by tag:

SORT contains(file.tags, "tagExample") DESC

a+

I did try the asc/desc option previously and still nothing is sorted.
I also tried your option with multiple sorts and it still is not sorting the list.

TASK FROM "" 
WHERE (contains(tags, "Y2023") 
AND contains(tags, "SP") 
AND contains(tags, "/P")) 
and !completed 
SORT contains(file.tags, "Y2023")  asc
SORT contains(file.tags, "/Q")  asc
SORT contains(file.tags, "SP")  asc
SORT contains(file.tags, "/P")  asc

Could somebody maybe provide the equivalent query in dataviewjs? Maybe sorting by tag would work with dataviewjs

The following seems to do the trick. Just need to modify it slightly to support all tags but the sorting does work. I just wish it worked with plan dataview query.

Here you’re sorting on whether the entire list of Tags contains various bits and parts. An alternate approach would be to filter out just the one you want to use for sorting.

Yet another approach would possibly be to filter out the one tag you want, and split it into its subtags which would possibly ease the where and sort clauses.

I’m on mobile now, so can’t write you the examples, just give you some hints.

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