Dataview Sort Example share and Question

I have multiple files in a “LOG” directory that I wanted to sort be log date, not creation date in missing log days were added at later dates. Here is what I came up with, fair warning I do not fully understand why it works in regard to the second choice.

The log files are named
ROBOT DAILY LOG 2024-09-XX

There is a YAML property “SORTING” that I define as “ASCENDING” or “DESCENDING”

TABLE reverse(substring(reverse(file.name),0,10)) AS "DATE"
FROM ""
WHERE contains(file.name, this.file.frontmatter.PROJ_NAME)
SORT choice(this.file.frontmatter.SORTING = "ASCENDING",
    reverse(substring(reverse(file.name),0,10)),
    DATE)

What I do not understand is why DATE, and reverse(DATE) or DATE ASC, DATE DESC
do not work.

If anyone knows a better way please let me know, I am hoping this not break in the future :slight_smile:

I came up this this as a more reliable solution:

let sorting = dv.current().SORTING;

if (sorting === "ASCENDING") {
    dv.table(["File", "Date"], 
        dv.pages("")
        .where(p => p.file.frontmatter.PROJ_NAME === dv.current().PROJ_NAME && p.file.name.includes(dv.current().PROJ_NAME))
        .sort(p => dv.date(p.file.name.slice(-10)), 'asc')
        .map(p => [
            dv.fileLink(p.file.name), // Create a link to the file
            dv.date(p.file.name.slice(-10)) // Extracted date
        ])
    );
} else if (sorting === "DESCENDING") {
    dv.table(["File", "Date"], 
        dv.pages("")
        .where(p => p.file.frontmatter.PROJ_NAME === dv.current().PROJ_NAME && p.file.name.includes(dv.current().PROJ_NAME))
        .sort(p => dv.date(p.file.name.slice(-10)), 'desc')
        .map(p => [
            dv.fileLink(p.file.name), // Create a link to the file
            dv.date(p.file.name.slice(-10)) // Extracted date
        ])
    );
}