Dataview not sorting results

I just can’t make this query sort the results.

list file.inlinks
where file=this.file

I tried sort by file.inlinks, file.name, file, file.mtime, file.ctime and even without any arguments (sort desc). File names start with dates (YYYY-MM-DD), so it should be very straightforward. I don’t even know what criteria it is using to sort. Just get messed results. Changing asc to desc changes nothing.

Either of these?

```dataview
list sort(file.inlinks) 
where file = this.file
sort file.name ASC
```

```dataview
list reverse(file.inlinks) 
where file = this.file
sort file.name DESC
```

Source:

First point to clarify:

  • your query is related with the current note: a list of inlinks to the current note (that’s why you use WHERE file = this.file
  • in the case, you don’t need in the list results the prefix «link of the current file». i.e., you don’t need
- current file:
  - inlink 1
  - inlink 2

but just

- inlink 1
- inlink 2
  • to have this type of “clean” list, as I said in your other post, you need to use something like this:
LIST WITHOUT ID InLinks
WHERE file.path = this.file.path
FLATTEN file.inlinks AS InLinks
  • use file.path = this.file.path instead of file = this.file because performance issues.

Now, the point related with sort:

  • if you use the query above, you can sort in this way:
LIST WITHOUT ID InLinks
WHERE file = this.file
FLATTEN file.inlinks AS InLinks
SORT InLinks DESC
  • but… Inlinks are links. So, sort links implies sort the “path”. And this can be a mess if files aren’t all in the same folder. In this cases you can go “through” the links and use a more reliable field in these links, as file.name or file.day (once you’re using a date format in title):
LIST WITHOUT ID InLinks
WHERE file = this.file
FLATTEN file.inlinks AS InLinks
SORT InLinks.file.day DESC

Play with these queries and check if it works.

2 Likes

Thanks!! Finally it worked!
I still do not understand the flatten concept. But now I get the results I want. The messed results might come from the folder structure. Or not, they’re named YYYY-MM so I don’t get too much files in each folder.
And thanks for the performance tip. I had no idea and I don’t think I would run into that information.

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