Table of tagged sections or pages

Some time ago, I asked how to get a list of section that have a certain tag, see List heading of tagged section, but I got no replies.

I tried to implement it myself, and it works with the dataviewjs code below:

const tag = "#MyTag"
const regex = new RegExp('#+ (.*)\n.*' + tag + '.*', 'g')
let table = []
for (let page of dv.pages(tag)) {
	const contents = await
	const matches = contents.matchAll(regex)
	let at_least_one_match = false
	for (const match of matches) {
		at_least_one_match = true
			dv.sectionLink(, match[1])])
	if (!at_least_one_match) {
		// this is to get pages where the tag is a property or a the very top.

dv.table(["Modified", ""], table.sort(a => a[0]))

This results in a table like

listing sections or pages (if a section for the tag is not found).

One Question: The sort statement does not seem to work. Any hints on that?

I am also open to any feedback and improvements of the code!



We’ve gotten accustomed to doing that sort which really is an dataview extension of javascript arrays. More on how to sort the latter is found here.

In essence it says that you need to do something like .sort( (a, b) => ... , where the ... is an expression which is negative if a should be before b, equal if they’re at the same position, and above zero/positive if a is after b. The two main variants of this sorting method is either .sort( (a, b) => a.localeCompare(b) used for strings, or .sort( (a, b) => a - b ) for numbers.

In your case the elements are date, which we can transform easily to numbers, so try the following in your sort() statement:

table.sort( (a, b) => a[0].ts - b[0].ts )

Switch the elements around if you want them in the revers order.