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
fix(sort): keep a child missing a level's label last in both directions
Elian Doran4cd5399a5d74
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.htmlindex e939d7c59c..70893bf2f2 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.html@@ -36,9 +36,9 @@spellcheck="false">desc</code>, as in a search's <code spellcheck="false">orderBy</code>.For example <code spellcheck="false">#sorted="priority desc, dueDate"</code> sortsby priority, highest first, and notes of equal priority by due date. A- child note without a level's label counts as the largest value there: last- ascending, first descending. When neither note has the label, the next- level decides.</p>+ child note without a level's label sorts after every child that has it,+ whichever direction the level runs in. When neither note has the label,+ the next level decides.</p></td></tr><tr>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 4de3872b9b..ce752ef7f9 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>, as in a search's <code spellcheck="false">orderBy</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 counts as the largest value there: last ascending, first descending. When neither note has the label, the next level decides.</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>, as in a search's <code spellcheck="false">orderBy</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 sorts after every child that has it, whichever direction the level runs in. When neither note has the label, the next level decides.</p></td></tr><tr><td><code spellcheck="false">#sortDirection</code></td>diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.tsindex c2de5f8efb..761a29cbdd 100644--- a/packages/trilium-core/src/services/tree.spec.ts+++ b/packages/trilium-core/src/services/tree.spec.ts@@ -255,16 +255,18 @@ describe("Tree", () => {expect(orderedTitles).toStrictEqual(["top1", "top2", "b", "a", "bottom2", "bottom1"]);});- it("counts a missing label as the largest value; the next level breaks a double miss", () => {+ it("sorts a child missing the label last in either direction; the next level breaks a double miss", () => {const children: Parameters<typeof buildNote>[0]["children"] = [{title: "m", "#order": "z"},{title: "unlabelled", "#area": "b"},{title: "also", "#area": "a"},{title: "a", "#order": "b"}];+ // Descending reverses the two notes that carry #order, and reverses the #area level that+ // separates the two that do not, but leaves both of those after the ones that carry it.for (const [direction, expected] of [["asc", ["a", "m", "also", "unlabelled"]],- ["desc", ["unlabelled", "also", "m", "a"]]+ ["desc", ["m", "a", "unlabelled", "also"]]] as const) {const note = buildNote({children, "#sorted": "order, area", "#sortDirection": directiondiff --git a/packages/trilium-core/src/services/tree.ts b/packages/trilium-core/src/services/tree.tsindex e76a30d1ca..2dbac4e4a9 100644--- a/packages/trilium-core/src/services/tree.ts+++ b/packages/trilium-core/src/services/tree.ts@@ -130,11 +130,19 @@ function sortNotes(return a < b ? -1 : a > b ? 1 : 0;}- // A child without the label counts as the largest value, and two without it tie so the- // next level decides.+ function compareReversed(a: string, b: string) {+ return -compare(a, b);+ }++ // A child without the label sorts after every child that has it, whichever direction the+ // level runs in, so only the values both children have follow `descending`. Two children+ // without it tie, leaving the next level to decide.function compareLevel(a: BNote, b: BNote, key: string, descending: boolean) {- const result = compareSortValues(fetchValue(a, key), fetchValue(b, key), compare);- return descending ? -result : result;+ return compareSortValues(+ fetchValue(a, key),+ fetchValue(b, key),+ descending ? compareReversed : compare+ );}notes.sort((a, b) => {