Help with group and sort with dataview plugin

Things I have tried

What I’m trying to do

Hey, loving Obsidian so far but I’m not a coder and I’m having trouble understanding how to use the Dataview plugin.

I have a folder named 6.Projects which contains a bunch of folders for each project I’m working on, plus an _Archive folder. Each of the project folders also contains an _Archive folder where I store single, completed notes re: a specific project once I’m done with them.

6.Projects
    _Archive
        Project_4
            _Archive
    Project_1
        _Archive
    Project_2
        _Archive
    Project_3
        _Archive

I’d like to use Dataview to grab a list of files, grouped by folders, excluding anything that’s into an _Archive folder. Is that even possible?

I’m trying to keep track of my ‘active’ projects and notes this way, without manually tagging files.

Edit: I got it kinda working with

```dataview
LIST rows.file.link FROM "6.Projects" WHERE !contains(file.folder, "_Archive") AND !contains(file.name, "_Working on")
GROUP BY file.folder
```

Now… how do I sort the results below each grouping? Right now they appear to be sorted randomly, while I’d like to sort them by filename. Adding SORT rows.file.name ASC didn’t do it.

Hi.

Your query:

```dataview
LIST rows.file.link
FROM "6.Projects"
WHERE !contains(file.folder, "_Archive") AND !contains(file.name, "_Working on")
GROUP BY file.folder
```

About sorting, some tips (with a “practical” language - I’m not coder too, just more one newbie…):

  1. When using group by, sorting is more complex. The main level to sort are the groups itself. Let’s say you want to sort the output of the groups (not the content in each group). To do that the first idea is: SORT file.folder ASC. Don’t ask me why, but with groups this doesn’t work. You need to do this: first, render group fields to other name using AS syntax (GROUP BY file.folder AS Example); second, sorting using the rendered name (SORT Example DESC)
```dataview
LIST rows.file.link
FROM "6.Projects"
WHERE !contains(file.folder, "_Archive") AND !contains(file.name, "_Working on")
GROUP BY file.folder AS Example
SORT Example DESC
```
  1. Sorting the elements inside a group (without using FLATTEN) demands other direct approach: not using the global SORT but sorting directly the array/list inside groups (in this case rows.file.link) using the function sort(), i.e.:
```dataview
LIST sort(rows.file.link)
FROM "6.Projects"
WHERE !contains(file.folder, "_Archive") AND !contains(file.name, "_Working on")
GROUP BY file.folder
```

sort() gives a default asc natural order. If you need a desc order, you need to use the reverse() function (most of the time together with sort() - reverse(sort(rows.file.link)) - something like “first it’s necessary to order asc and then reverse that order”).

https://blacksmithgu.github.io/obsidian-dataview/query/functions/#sortlist

I hope this helps.

6 Likes

This is perfect, thanks for the detailed explanation :smiley:

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