Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
Revert "feat(core): sort children missing a level's label after those that have it"
BeatLinkdfed61d223f1
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.mdindex fd43efef66..3e6cbfe1c1 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. 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>+ <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></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. 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.+ * **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.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 filediff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.tsindex fca6739415..347be489ec 100644--- a/packages/trilium-core/src/services/tree.spec.ts+++ b/packages/trilium-core/src/services/tree.spec.ts@@ -253,41 +253,20 @@ describe("Tree", () => {expect(orderedTitles).toStrictEqual(["top1", "top2", "b", "a", "bottom2", "bottom1"]);});- 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", () => {+ it("sorts a child without the level's label by its title in the label's place", () => {const note = buildNote({children: [- {title: "z", "#priority": "1"},- {title: "b", "#dueDate": "2026-02-01"},- {title: "c", "#dueDate": "2026-01-01"},- {title: "a"}+ {title: "m", "#order": "z"},+ {title: "unlabelled"},+ {title: "a", "#order": "b"}],- "#sorted": "priority,dueDate"+ "#sorted": "order"});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(["z", "c", "b", "a"]);+ expect(orderedTitles).toStrictEqual(["a", "unlabelled", "m"]);});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.tsindex c3fb1a3a31..512c567585 100644--- a/packages/trilium-core/src/services/tree.ts+++ b/packages/trilium-core/src/services/tree.ts@@ -154,14 +154,10 @@ function sortNotes(return a < b ? -1 : a > b ? 1 : 0;}- // 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.+ // A child without the level's label sorts by its title in that label's place.function compareLevel(a: BNote, b: BNote, key: string, descending: boolean) {- 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 valueA = fetchValue(a, key) ?? fetchValue(a, "title") ?? "";+ const valueB = fetchValue(b, key) ?? fetchValue(b, "title") ?? "";const result = compare(valueA, valueB);return descending ? -result : result;}