Skip to content

Sort Explorer items by date modified #2267

@idevchandra

Description

@idevchandra

Describe the bug

I tried below code to sort the Explorer items by markdown file modified date. But the sorting is still by default alphabetical order.

To Reproduce

Steps to reproduce the behavior:

  1. Go to quartz.layout.ts
  2. Add below code to Component.Explorer({}) block to sort the markdown files by file modified date.
    Component.Explorer({
      sortFn: (a, b) => {
        // 1. Sort folders before files
        if (a.isFolder && !b.isFolder) return -1;
        if (!a.isFolder && b.isFolder) return 1;

        // 2. Sort by date if both are files
        if (a.data?.frontmatter?.date && b.data?.frontmatter?.date) {
          const dateA = new Date(a.data.frontmatter.date);
          const dateB = new Date(b.data.frontmatter.date);
          
          // Use (dateB - dateA) for newest first, or (dateA - dateB) for oldest first
          return dateB.getTime() - dateA.getTime();
        }
        const d1 = new Date(a.dates?.created ?? 0).getTime()
        const d2 = new Date(a.dates?.created ?? 0).getTime()
        return d2 - d1


        // 3. Fallback to alphabetical if date is missing or items are folders
        return a.displayName.localeCompare(b.displayName, undefined, {
          numeric: true,
          sensitivity: "base",
        });
      },
    }),
  1. Have similar frontmatter in the posts
---
title: My Post Title
date: 2025-12-10 11:58:47 +00:00
tags:
  - tech
---

What to expect

Build the website and see if Explorer shows the items in the expected order.

What is happening

The markdown files still shows in Explorer in alphabetical order.

Desktop (please complete the following information):

  • Quartz Version: v4.1.2
  • node Version: v24.10.0
  • npm version: 11.6.0
  • OS: macOS
  • Browser: chrome, safari, brave, firefox

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions