The Custom Sort plugin for Obsidian lets you dictate EXACTLY how you want your core File Explorer plugin to display your notes and folders. A sortspec.md file in your root vault folder can single handedly control the sorting of every folder in your vault by letting you:
- Manually sort items
- Dynamically sort items using various wildcard variables
- Group items that don't match any of the sorting rules
- Group notes and folders together or separately
The SORTSPEC File
The plugin is powered by the sortspec.md file that can live in the root of your vault or within a subfolder inside your vault. The sortspec.md file can be set up to control the folder that is resides in or it can target subfolders from there. The directions that the plugin uses to order your file explorer are all contained in the sortspec.md files.
A simple sortspec.md could be something like this:
---
sorting-spec: |
Matthew
Dave
Lise and Beth
Catherine
---
This sortspec.md file will look for any file or folder with that exact name and order them in the same order they are listed in the sortspec.md file regardless of how you've told Obsidian to order your vault. It can be that simple!
Here is the sortspec.md file seen in the image above.
---
sorting-spec: |
target-folder: /
/:files
< Modified
/folders
... \d+
sortspec
target-folder: My Book 04
Preface
Chapter \R+ ...
< a-z
Epi...
---
Let's briefly go through some of the lines.
- The "target-folder: /" means that the rules below it will apply to the root "/" of your vault.
- Next, we have "/:files" and "/folders" which each tell the plugin that the indented rules below them apply to only files or folders, respectively. If these lines are omitted the rules will be applied all items in the folder.
- The "< Modified" line will simply order every file in the root of your vault by modified time.
- The second "target-folder:" line is telling the plugin that the following indented rules will apply to the subfolder titled "My Book 04"
- In some cases you can see some regular words, like "Preface" which is the first rule under the "My Book 04" target-folder line. This will put any file or folder named "Preface" at the top of the folder, as that is the top rule listed.
- The less obvious rules are using wildcards to match to files and folders.
Basic Wildcards
"..." will match any amount of characters. For example, the rule "J...J..." would match files in your vault named both "Jimmy James" and "Johnny Johnson" while the rule "J...on" would only match the file named "Johnny Johnson".
"%" is a catch all wildcard that will match to any file that other rules don't already match to.
---
sorting-spec: |
Matthew
%
Dave
Lise and Beth
Catherine
---
This rule set would order those named files in that order and then cluster every other file that does not match those names between the files "Matthew" and "Dave". So both "Jimmy James" and "Johnny Johnson" would be listed where the "%" is in the sortspec.md file.
Numerical Wildcards
That sortspec.md from the video has two numerical wildcards.
Matching Digits
The "\d+" will match to any number. The rule "... \d+" will match ANY file or folder with a number at the end. It is already fairly common practice to preface folder names with numbers in Obsidian to control the order and now you can still use that method but place the index number at the end of the name.
So a sortspec.md with only this in it:
---
sorting-spec: |
... \d+
---
Will sort these folders by the number at the end of them instead of alphabetically:
Inbox 01
Fleeting Notes 02
Literature Notes 03
Permenent Notes 04
Creative 05
Structure 06
Templates 07
Using a wildcard to match the numbers at the end, instead of explicitly listing the names in the order you want them to appear in the sortspec.md file, gives you the flexibility to reorder your folders just by renaming them. No editing the sortspec.md file at all.
Matching Roman Numerals
Likewise, the wildcard "\R+" can be used to sort files and folders by ordering through Roman Numerals. This is a little more niche but has its uses. Let's look at that sortspec.md from the video again.
That has the folder "My Book 04" which has the sorting rules:
---
target-folder: My Book 04
Preface
Chapter \R+ ...
< a-z
Epi...
---
When custom sort is toggled on, that "My Book 04" folder will be sorted like in this screen shot:
As you can see, Preface is at the top, Epilogue is at the bottom, and between those two we have the Chapter files that are organized by the Roman Numerals in the middle of the file names.
Ordering by Frontmatter
Here we have a sortspec.md file in its own folder.
---
sorting-spec: |
target-folder: A Frontmatter Sort 05
< a-z by-metadata: InputOrder
---
Then, each file in that folder will have the YAML frontmatter with the corresponding key listed in the sortspec.md.
---
InputOrder: 4
---
and the plugin will use that frontmatter key/value pair to order the contents of that folder.