Skip to content

Build your personal knowledge base with Trilium Notes

feat(core): sort children missing a level's label after those that have it

BeatLinkc6d17f3b23f5
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
index 3e6cbfe1c1..fd43efef66 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md
@@ -24,7 +24,7 @@ Child notes can be automatically sorted by attaching specific [labels](../../Adv
<tbody>
<tr>
<td><code spellcheck="false">#sorted</code></td>
- <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each <code spellcheck="false">title</code>, <code spellcheck="false">dateCreated</code>, <code spellcheck="false">dateModified</code> or the name of a label on the child notes, optionally followed by <code spellcheck="false">:asc</code> or <code spellcheck="false">:desc</code>. For example <code spellcheck="false">#sorted="priority:desc,dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. If one of the child notes doesn't have a level's label, its title is used in its place.</p></td>
+ <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each <code spellcheck="false">title</code>, <code spellcheck="false">dateCreated</code>, <code spellcheck="false">dateModified</code> or the name of a label on the child notes, optionally followed by <code spellcheck="false">:asc</code> or <code spellcheck="false">:desc</code>. For example <code spellcheck="false">#sorted="priority:desc,dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. A child note without a level's label comes after every note that has it, in either direction; when neither note has it, the next level decides. A misspelt direction such as <code spellcheck="false">dueDate:acs</code> is read as a label name, which no note has, so that level changes nothing.</p></td>
</tr>
<tr>
<td><code spellcheck="false">#sortDirection</code></td>
@@ -61,7 +61,7 @@ Sorting is done by comparing note properties or specific labels on child notes.
* **Default Sorting**: If `#sorted` has no value, notes are sorted alphabetically.
* **Property Sorting**: If `#sorted` is set to `title`, `dateModified`, or `dateCreated`, notes are sorted based on the specified property.
* **Label Sorting**: If `#sorted` has any other value, this value is treated as the name of a child note's label, and sorting is based on the values of this label. For example, setting `#sorted=myOrder` on the parent note and using `#myOrder=001`, `#myOrder=002`, etc., on child notes.
- * **Multi-Level Sorting**: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. Every level can carry its own direction with `:asc` or `:desc`, otherwise it follows `#sortDirection`. For example `#sorted="priority:desc,area,dateCreated"` sorts by priority, highest first, then by area, then by creation date.
+ * **Multi-Level Sorting**: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. Every level can carry its own direction with `:asc` or `:desc`, otherwise it follows `#sortDirection`. For example `#sorted="priority:desc,area,dateCreated"` sorts by priority, highest first, then by area, then by creation date. A note missing a level's label sorts after the notes that have it, whatever the direction; two notes both missing it are left to the next level.
4. **Alphabetical Sorting**: Used as a last resort when other criteria result in equality.
All comparisons are made string-wise (e.g., "1" \< "2" or "2020-10-10" < "2021-01-15", but also "2" \> "10").
\ No newline at end of file
diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.ts
index 347be489ec..fca6739415 100644
--- a/packages/trilium-core/src/services/tree.spec.ts
+++ b/packages/trilium-core/src/services/tree.spec.ts
@@ -253,20 +253,41 @@ describe("Tree", () => {
expect(orderedTitles).toStrictEqual(["top1", "top2", "b", "a", "bottom2", "bottom1"]);
});
- it("sorts a child without the level's label by its title in the label's place", () => {
+ it("sorts children without a level's label after those with it, whatever the direction", () => {
+ const children: Parameters<typeof buildNote>[0]["children"] = [
+ {title: "m", "#order": "z"},
+ {title: "unlabelled"},
+ {title: "a", "#order": "b"}
+ ];
+ for (const [direction, expected] of [
+ ["asc", ["a", "m", "unlabelled"]],
+ ["desc", ["m", "a", "unlabelled"]]
+ ] as const) {
+ const note = buildNote({ children, "#sorted": "order", "#sortDirection": direction });
+ getContext().init(() => {
+ tree.sortNotesIfNeeded(note.noteId);
+ });
+ expect(note.children.map((child) => child.title)).toStrictEqual([...expected]);
+ }
+ });
+
+ it("leaves children that both lack a level's label to the next level", () => {
const note = buildNote({
children: [
- {title: "m", "#order": "z"},
- {title: "unlabelled"},
- {title: "a", "#order": "b"}
+ {title: "z", "#priority": "1"},
+ {title: "b", "#dueDate": "2026-02-01"},
+ {title: "c", "#dueDate": "2026-01-01"},
+ {title: "a"}
],
- "#sorted": "order"
+ "#sorted": "priority,dueDate"
});
getContext().init(() => {
tree.sortNotesIfNeeded(note.noteId);
});
+ // Only "z" has a priority; the other three fall through to dueDate, and "a", lacking that
+ // too, comes last rather than being placed by its title ahead of them.
const orderedTitles = note.children.map((child) => child.title);
- expect(orderedTitles).toStrictEqual(["a", "unlabelled", "m"]);
+ expect(orderedTitles).toStrictEqual(["z", "c", "b", "a"]);
});
it("parses a #sorted value into its levels", () => {
diff --git a/packages/trilium-core/src/services/tree.ts b/packages/trilium-core/src/services/tree.ts
index 512c567585..c3fb1a3a31 100644
--- a/packages/trilium-core/src/services/tree.ts
+++ b/packages/trilium-core/src/services/tree.ts
@@ -154,10 +154,14 @@ function sortNotes(
return a < b ? -1 : a > b ? 1 : 0;
}
- // A child without the level's label sorts by its title in that label's place.
+ // A child without the level's label sorts after every child that has it, whatever the
+ // direction; two children without it are left for the next level to order.
function compareLevel(a: BNote, b: BNote, key: string, descending: boolean) {
- const valueA = fetchValue(a, key) ?? fetchValue(a, "title") ?? "";
- const valueB = fetchValue(b, key) ?? fetchValue(b, "title") ?? "";
+ const valueA = fetchValue(a, key);
+ const valueB = fetchValue(b, key);
+ if (valueA === null || valueB === null) {
+ return (valueA === null ? 1 : 0) - (valueB === null ? 1 : 0);
+ }
const result = compare(valueA, valueB);
return descending ? -result : result;
}