Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/sorting: tweak sorting rules

Adorian Doranaef3ad6bb7a4
diff --git a/apps/client/src/widgets/collections/sorting.spec.ts b/apps/client/src/widgets/collections/sorting.spec.ts
index f2c3fdd42a..1bc69d76c3 100644
--- a/apps/client/src/widgets/collections/sorting.spec.ts
+++ b/apps/client/src/widgets/collections/sorting.spec.ts
@@ -97,15 +97,23 @@ describe("sortItems", () => {
expect(titles(sort(times, "attr:field", false, "time"))).toEqual([ "C", "B", "A" ]);
});
- it("sorts false before true, and reads a label with no value as true", () => {
+ /**
+ * An unchecked box is stored as "false" once the user has touched it and as nothing at all
+ * before that, so a boolean with no label must not sort with the values that are missing.
+ */
+ it("sorts an unset boolean with false, and reads a label with no value as true", () => {
const items = build([
{ title: "Bare", "#field": "" },
+ { title: "Untouched" },
{ title: "True", "#field": "true" },
{ title: "False", "#field": "false" }
]);
expect(titles(sort(items, "attr:field", false, "boolean")))
- .toEqual([ "False", "Bare", "True" ]);
+ .toEqual([ "Untouched", "False", "Bare", "True" ]);
+ // Last in both directions is what a missing value does; nothing here is missing.
+ expect(titles(sort(items, "attr:field", true, "boolean")))
+ .toEqual([ "Bare", "True", "Untouched", "False" ]);
});
it("sorts a select by the order its options are declared in, not alphabetically", () => {
@@ -148,16 +156,23 @@ describe("sortItems", () => {
expect(titles(sort(items, "attr:field", false, "select", []))).toEqual([ "B", "A" ]);
});
- it("sorts colours by hue and leaves grey with the items that have no value", () => {
+ it("sorts the greys ahead of the hues, and a colour it cannot read behind both", () => {
const items = build([
{ title: "Blue", "#field": "#0000ff" },
{ title: "Red", "#field": "red" },
{ title: "Grey", "#field": "#808080" },
- { title: "Green", "#field": "#00ff00" }
+ { title: "Green", "#field": "#00ff00" },
+ { title: "Black", "#field": "#000" },
+ { title: "Unreadable", "#field": "not a colour" },
+ { title: "None" }
]);
expect(titles(sort(items, "attr:field", false, "color")))
- .toEqual([ "Red", "Green", "Blue", "Grey" ]);
+ .toEqual([ "Grey", "Black", "Red", "Green", "Blue", "Unreadable", "None" ]);
+ // The greys are a value like any other, so they turn over with the hues; only the items
+ // with no colour to read stay last.
+ expect(titles(sort(items, "attr:field", true, "color")))
+ .toEqual([ "Blue", "Green", "Red", "Grey", "Black", "Unreadable", "None" ]);
});
it("sorts relations by the title of the note they point at", () => {
diff --git a/apps/client/src/widgets/collections/sorting.ts b/apps/client/src/widgets/collections/sorting.ts
index 3b3a4c1e09..59d30e50d3 100644
--- a/apps/client/src/widgets/collections/sorting.ts
+++ b/apps/client/src/widgets/collections/sorting.ts
@@ -110,13 +110,17 @@ function sortValueOf(note: FNote, key: SortKey, context: SortContext): SortValue
// The first value of a multi-value attribute, which is the one an item draws.
const label = note.getLabel(name);
- if (!label) {
- return undefined;
- }
- // Trilium reads a label written without a value as true, so `#done` sorts as true.
+ // A boolean has no third state. The editor writes "false" for a box the user unchecked and
+ // leaves no label at all on a note nobody has touched, and both draw as an unchecked box, so
+ // neither may fall to the undefined group. An empty value is truthy, as `isLabelTruthy` reads
+ // it.
if (definition?.labelType === "boolean") {
- return label.value === "false" ? 0 : 1;
+ return label && label.value !== "false" ? 1 : 0;
+ }
+
+ if (!label) {
+ return undefined;
}
return label.value ? labelValueOf(label.value, definition) : undefined;
@@ -172,11 +176,17 @@ function secondsOfDay(value: string) {
return Number(parts[1]) * 3600 + Number(parts[2]) * 60 + Number(parts[3] ?? 0);
}
-/** The hue of a colour. Grey has none, so it sorts with the items that have no value. */
+/** Below every hue, which runs 0 to 360, for the greys that have none. */
+const GREY = -1;
+
+/**
+ * The hue of a colour, or {@link GREY} for one with no saturation. A value nothing can be read
+ * from has no key at all, which sorts it with the items that carry no colour.
+ */
function hueOf(value: string) {
try {
const color = Color(value.toLowerCase()).hsl();
- return color.saturationl() > 0 ? color.hue() : undefined;
+ return color.saturationl() > 0 ? color.hue() : GREY;
} catch {
return undefined;
}