Main problem is related with the sort()command.
Tasks query in JS doesn’t works as in DQL. In DQL it works at same time in both levels: page level and task level. That’s why you can sort by page level data (file.mtime).
But in js each step downs the level… For example: in dv.pages() you’re in page level, but after dv.pages().file.tasks you’re in tasks level… From this moment you can’t target a page level data in normal way. That’s why .sort(f=>f.file.mtime.ts,"desc") at the end of the code doesn’t works.
And the default group by file link add an extra problem… I still don’t know how to solve the sort inside this default behavior).
But if you don’t want the headers (using false as mentioned by @holroy), then it’s more easy to solve.
The most easy way is doing the sort at the page level before “jump” to tasks level.
Something like:
Wow, That does make sense I used .sort(f=>f.task.mtime,"desc") which fixed the sort issue but your way is much cleaner and false by @ glory solves the headache of the heading