Help me to improve these dataview codes

I use the following codes to enqury all the pages and blocks containing the title of page, Could you help me to sort the results.

const keyword = dv.current().file.name
const all_files = app.vault.getMarkdownFiles()
    var path = []
const files_showaspage = []
const files_showasblock = []
all_files.forEach( (file) =>{
	path = app.vault.getResourcePath(file)
    if (
        path.contains("journals")
    ){}else{
        if (path.split("/").pop().contains(keyword))
        {files_showaspage.push(file)}
        else{files_showasblock.push(file)}
    }
})

const showpage = await Promise.all(files_showaspage.map(async(file) => {
	return "- [["+file.name.split(".")[0]+"]]"
}))
dv.paragraph(showpage)

const showblock = files_showasblock.map( async(file) => {
    const content = await app.vault.cachedRead(file)
    const block = content.split(/\n\n|^#/).filter(c => c.contains(keyword))
    return ["[["+file.name.split(".")[0]+"]]",block]
})
Promise.all(showblock).then( values => {
	const blocks = values.filter(value => value[1][0])
	dv.table(["file","content"],blocks)  //I want to sort the content
})

Why do you use native javascript to list and filter the files instead of some variant of a dv.pages('"journals"') combined with filter() and/or map() ? That could in some cases allow you to use its built-in sorting, doing something like sort(a => a.something).

However, you are reading the actual contents of some of these file, which could be cumbersome to do in that context. But this also means you’ll have to resort to ordinary javascript sorting, and that is to specify the sort method to use.

So instead of doing sort( a => a.something ), you’ll need to do blocks.sort( (a, b) => ... ) where ... should return -1 (or something negative) if a is before b, 0 if at the same position, and 1 (or something above zero) when a is after b.

Using numbers this could look like blocks( (a, b) => a - b ), or using strings as blocks( (a, b) => a.localeCompare(b) ). The latter uses a locale version of the string compare allowing for international characters to be sorted correctely according to your locale settings. a and b are two “random” elements from your array which it’s about to compare.

You could append the .sort() statement after your .filter() statement, resulting in something like the following (given that they’re strings):

const blocks = values
  .filter( value => value[1][0] )
  .sort( (a, b) => a.localeCompare(b) )

Well, all block results disappeard after adding .sort( (a, b) => a.localeCompare(b) )

Anyway, the GPT helped me =_=|||. The following codes works:

const keyword = dv.current().file.name
const all_files = app.vault.getMarkdownFiles()
var path = []
const files_showaspage = []
const files_showasblock = []
all_files.forEach((file) => {
    path = app.vault.getResourcePath(file)
    if (path.contains("journals")) {} else {
        if (path.split("/").pop().contains(keyword)) {
            files_showaspage.push(file)
        } else {
            files_showasblock.push(file)
        }
    }
})

const showpage = await Promise.all(files_showaspage.map(async (file) => {
    return "- [[" + file.name.split(".")[0] + "]]"
}))
dv.paragraph(showpage)

const showblock = files_showasblock.map(async (file) => {
    const content = await app.vault.cachedRead(file)
    const blocks = content.split(/\n\n|^#/).filter(c => c.contains(keyword))
    const sortedBlocks = blocks.map(block => {
        const date = block.split("\n")[0].replace("%%", "")
        return [date, block, "[[" + file.name.split(".")[0] + "]]"]
    })
    return sortedBlocks
})
Promise.all(showblock).then(values => {
    const blocks = values.flat().filter(value => value[1])
    blocks.sort((a, b) => {
        const dateA = a[0]
        const dateB = b[0]
        if (dateA < dateB) {
            return -1
        }
        if (dateA > dateB) {
            return 1
        }
        return 0
    })
    const sortedBlocks = blocks.map(block => [block[1], block[2]])
    dv.table(["content", "file"], sortedBlocks)
})

It works only on my specific notes. I used to adding %%YYYYMMDD-hhmmss%% at the beginning of each blocks. These codes helps me to find each blocks and rank it according to time.

The reason is that I don’t know how to …