Sorting Dataview file.inlinks

Hi all

Please excuse questionable terminology but hopefully you can see what I’m trying to ask.

When creating an array of backlinks like so:

list file.inlinks
from [[Example file title]]
sort file.name ASC

The “top level links” are sorted alphabetically A-Z but the “sublinks” are sorted non alphabetically. I suspect by date created.

Does anyone know if or how the sublinks can also have an alphabetical sort order?

Thanks.

1 Like

Oh this is neat, your query captures not only the backlinks of “Example file title” but also the backlinks of the backlinks!

I think I got correct sorting by replacing list file.inlinks with:

list sort(file.inlinks)

To sort in the other direction replace sort with reverse according to the docs.

Does that work for your notes?

Thanks scholarInTraining. Curiously it does work 100% on most of the files but not all.

I had:
d
a
e
b
c
f

Most files:
a
b
c
d
e
f

Some files:
c
d
e
f
a
b

Something related to the metadata contained within the notes?

Hmm - could be! I’ll investigate a bit along with you. My usual investigation strategies:

  • turn list into TABLE and make columns out of anything that might be relevant/interesting (e.g. can you call meta on the inlinks and get anything out?)
  • switch to javascript and start printing things out to the console in their intermediate formats

I think the first option might have the most potential here.
Will edit this post if I come up with anything!

EDIT: @Visitor_82 Possibly useful information: links compare by full path, not by filename. Does that explain any of the things you were seeing?

1 Like

Something to try:

LIST sort(map(file.inlinks, (theLink) => theLink.file.name))

This will make the sublinks (and only the sublinks) no longer links, but does that sort them correctly?
If so, you can probably get the linking back by wrapping the whole thing in another map:

LIST map(sort(map(file.inlinks, (theLink) => theLink.file.name)), (f) => link(f))

Note that this will have problems if you have two files with the same name in different places in your vault. link is expecting its argument to be a full path, not a filename. Obsidian and/or dataview seem to be smart about making links from just filenames work as long as your filenames are unique in your vault, but if you have two files with the same name there’s no way for it to know which one you want to link to without more information. Is this a situation you have to worry about for your vault?

1 Like

That works!!! Thank you.

My head is about to explode trying to decipher what’s going on in that query but I’ll get there :wink:

90% of my files go into a general folder - and all have unique names anyway - so I shouldn’t run into any problems on that front.

I’ll post again if I discover anything of interest going forward.

Thanks again.

I felt the same way trying to write it! If you aren’t familiar with map() and “arrow functions” those are concepts that are shared across several programming languages so there should be explanations that fit your way of thinking about things somewhere. For a one sentence version: map transforms a list by applying the specified transformation to every item in the list, keeping the items in the order they were already in. So we had a list of file.inlinks and we transformed them into a list of filenames. Then we sorted the list of file names. Then we transformed the sorted list of file names to be a list of links again.
Fun fact: any time your query uses the . notation for dataview fields such as file.inlinks, dataview is essentially doing a map operation behind the scenes, e.g. to turn a list of file objects into a list of each file’s inlinks! (This implies a question of when we can use the . notation as a shortcut and when we have to use map explicitly. I don’t know the answer: so far my approach has been entirely trial & error!)

1 Like

Much appreciated you going above and beyond on this one :clap:

1 Like

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