In dataview, How can I group tasks in a task query by file.link and make them show up in alphabetical order?

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

I have a task query like this:

```dataview
TASK
WHERE regexmatch("^\d\d\d\d-\d\d-\d\d (January|February|March|April|May|June|July|August|September|October|November|December)", file.name)
WHERE contains(list(" "), status) AND contains(tags, "")
GROUP BY file.link
SORT file.name desc
```

Which shows the files in this order:

  • 2023-04-27 April 27th 2023 Thursday
  • 2023-04-28 April 28th 2023 Friday
  • 2023-05-05 May 5th 2023 Friday
  • 2023-05-06 May 6th 2023 Saturday

When I want it in this order:

  • 2023-05-06 May 6th 2023 Saturday
  • 2023-05-05 May 5th 2023 Friday
  • 2023-04-28 April 28th 2023 Friday
  • 2023-04-27 April 27th 2023 Thursday

Things I have tried

I’ve tried changing it to the following sorts but they didn’t work either: SORT key desc, sort file.name desc.
I previously had sorted them by file.name and it worked:

```dataview
TASK
WHERE regexmatch("^\d\d\d\d-\d\d-\d\d (January|February|March|April|May|June|July|August|September|October|November|December)", file.name)
WHERE contains(list(" "), status) AND contains(tags, "")
GROUP BY file.name
SORT key desc
```

Wild guess:

SORT
rows.file.day DESC
1 Like

Do you want to sort the tasks themselves, or the groups of tasks? Your example seems to indicate you want to sort the groups of tasks, but is that a correct interpretation?

Thanks! SORT rows.file.day DESC successfully sorted all of them in alphabetical / chronological order. Was that actually a wild guess or sarcasm? It would be funny if that actually was sarcasm and you were hinting at me to look at the documentation.

@holroy I should came here minutes ago to write this lol.

Here are questions for both of you:

  • What should I do if I have issues like this with Dataview, or with Datacore whenever the hell that comes out? Wouldn’t you expect either SORT key desc, SORT file.name desc, SORT file.link desc to work?
  • Is there any part of the documentation I could’ve looked at to figure it out.
  • From my lesser experience of Dataview (lesser compared to lot of other users, and both of you apparently), it seems like things that should work or work in theory like the sorting lines I wrote, they don’t. What do you think?
  • In a way, the documentation just doesn’t feel right, like it feels intimidating I guess, like even if I look through it over and over again I still feel clueless despite getting the jist. I wonder if it has to do with the design of it, would it be better if the documentation site was prettier like if it used another software like say, Docusaurus?
1 Like

Even though this works, it isn’t a very clean solution as you’re sorting on arrays, which can be a slightly treacherous thing to sort on.

Even just doing SORT rows[0].file.day DESC would be somewhat safer, as it then picks the date from whatever file is the first in that set. Similarly, you could also do the SORT rows[0].file.name DESC to actually get the file name of the first file in your set.

If you would like to use the actual key, which in this case is a link, you would need to do something like: SORT regexreplace( key, "(?:.*/)?([^/]+)\.md", "$1") ) DESC. Then you wouldn’t rely on anything in the rows element.

So @anon63144152 didn’t really use neither sarcasm or wild guesses, I reckon, he just presented a solution which can possibly have side effects, and which could work nicely in most cases.

You need to understand what you change, and how you change stuff. In your case you changed from a simple string, file.name, to a link, file.link. Visually they kind of look the same, but when used in comparison they’re not the same, and you need to know how they differ.

Within a DQL query, you could try doing meta(file.link) in a table query, and see some of the sub-information available. I’m not quite sure what’s actually used for the sorting when sorting a link, but in most cases it would at least include the folder information of your notes, and possibly also other stuff like heading/section information to mention some.

So to answer your question: No, I wouldn’t expect links to sort the same as strings, as they’re a different beast, and you need to specify which part/subset of the link you want to use for sorting.

Well, hard question as it related to the total understanding of variables and different type of variables. There are hints in the documentation that you can indeed use meta on the links to get to the various parts, but in some cases one do need experience to detect differences like these.

One way to tackle issues like these are to use more debug information, and especially in this case possibly to transform your task query into a table query and display the various options as columns. Then you could be able to see how the file.name and file.link differ, and possibly recognise one as a string and one as a link. (So some tools related to this would be to use stuff like typeof(file.link), and after reading documentation/plundering for a while, you could get the meta(file.link) )

The documentation is very concise, and maybe not extensive enough to cover various alternatives and examples related to all the vaste possibilities that Dataview offers. I don’t think the software used for the documentation site is the use, it’s more to do with the chosen level of documentation, and the “simple” issue of how to document something as capable of Dataview in an easy to understand and precise language, readily available both for the novice and the not-so-novice end-user.

1 Like

No sarcasm intended at all. Apologies if it sounded like that. Would never want to offend.

I have almost no knowledge in terms of Dataview, so it really was a wild guess and just some blundering around in a local test that eventually seemed to work … but all much improved by @holroy

2 Likes

If you would like to use the actual key, which in this case is a link, you would need to do something like: SORT regexreplace( key, "(?:.*/)?([^/]+)\.md", "$1") ) DESC. Then you wouldn’t rely on anything in the rows element.

I don’t care exactly how its done as long as it works and it doesn’t lag. Which way to sort do you recommend, and think is best or optimal? In terms of best practices for writing queries, behavior / functionality, and or performance.

Also, what about the SORT rows.file.day DESC is treacherous?

If you use any of the alternatives I suggested, you’re sorting a list of four values:

  • 2023-04-27 April 27th 2023 Thursday
  • 2023-04-28 April 28th 2023 Friday
  • 2023-05-05 May 5th 2023 Friday
  • 2023-05-06 May 6th 2023 Saturday

With rows.file.day you’re doing a sort on four array of varying length:

  • An array with length equal to the number of tasks for this day: 2023-04-27 April 27th 2023 Thursday, where each repeated value is the date 2023-04-27
  • An array with length equal to the number of tasks for this day: 2023-04-28 April 28th 2023 Friday, where each repeated value is the date 2023-04-28
  • An array with length equal to the number of tasks for this day: 2023-05-05 May 5th 2023 Friday, where each repeated value is the date 2023-05-05
  • An array with length equal to the number of tasks for this day: 2023-05-06 May 6th 2023 Saturday, where each repeated value is the date 2023-05-06

So instead of sorting on four values, you’re sorting on the entire set of dates related to the number of tasks you’ve got split into four arrays. Given a high enough number of tasks, this becomes treacherous as I see it. I’m also a little unsure on how effective it’ll be calculating the rows.file.day when thinking about how sorting work and it has to calculate the sorting value (aka the array of values) over and over again.

Of course when the numbers will increase, and the effect of sorting an array of arrays, versus an array of single values, the effect will only be more and more present…

1 Like

OK then which of the sorts you wrote would you recommend?

I think either one of my options would work nicely, but maybe I would choose the SORT rows[0].file.name DESC, since it’s computational simple, allows for the non-date file names, and it’s relatively simple to understand what’s used for the sorting.

The file.day variant is also nice, but it looses the “first” place due to the potential of files missing the date component, and thusly rendering the sorting back to the default order.

The regex-variant is possibly the “most correct” variant, if such a phrase exists, but it is slightly heavier from a computational point of view, and it’s not very easy to see that it’s actually sorting on the date within the file name, unless you’re familiar with reading regex’s.

1 Like

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