I hope this question wasn’t asked already. I searched the forum, but didn’t find anything similar to my issue.
Setup
I use a vague Zettelkasten-System in my notes. Each note begins with an index number:
1
1.1
1.2
1.2.1
...
74.1.1
74.1.2
...
74.1.10
I now want to have an Index note with a Dataview-Table. The entries should be sorted according to their Index-Number.
This is my current dataview query:
TABLE Typ, Thema, Status, URL, Permanent_Copy
WHERE Typ
SORT (file.name) ASC
Problem
SORT does not sort like this
74.1.1
74.1.2
74.1.10
but instead
74.1.1
74.1.10
74.1.2
Things I have tried
I tried to add an aditional entry
Index: $Indexnumber
because I thought it was only an issue in file names. Didn’t help. I tried different separators aside from “.”, but also the same result. In my normal file list in the sidebar the sorting is correct.
My question therefor would be: How do I achieve the same sorting as Obsidian does in the file list in the sidebar in my dataview query?
What this means? You have more “content” in the title?
If your titles are only in the format you wrote above (without more words), then you have a “rule” and we can use a tricky way to achieve the wanted sort. If more words… well, depends. (maybe using a field “index” number)
Thanks for your fast reply. The note title has more content.
74.1 Title ABC
74.1.1 Title DEF
74.1.2 Title GHI
I can try
SORT number(split(file.name, "\.")[0]) ASC
with an additional Index-Key, but it looks like it only works when I have only one “.”? I don’t have the experience to adapt it to a possible infinite amount of dots. I probably have to sort it first for the first level (e.g. 74.), then sort for second (.1.) and then for the third (.2), right?
It would be nice to have a solution that works with an indefinite amount of separators, but for the moment, I can adapt just this manually on the go. I guess at some point I will have enough hierarchies to work with.
You can do that with DataviewJS query, but it’s significantly more verbose than @mnvwvnm 's tidy one.
Split the index number into blocks, and turn those into blocks of 1000, and turn the final number into an integer. So 1.2.3 would end up as 1002003. That way you can numerically sort them.
```dataviewjs
function convertToNumericIndex (name) {
// Split by space to get the index number
let index = name.split(' ')[0]
// Split the index on '.' to get an array of parts
index = index.split('.')
// Turn the index into an integer padded by thousands
return index
.map((x, i) => +x * Math.pow(1000, index.length - i))
.reduce((curr, prev) => curr + prev)
}
dv.table([], dv.pages()
.sort(x => convertToNumericIndex(x.file.name), 'asc')
.map(page => [page.file.name, page.file.link]))
```