// lib/group.js — group saved assignments by subject for the sidebar tree and // the Library page. Shared so both stay in sync. export function groupBySubject(items) { const map = new Map(); for (const a of items || []) { const key = (a.subject && a.subject.trim()) || a.gradeLevel || "Other"; if (!map.has(key)) map.set(key, []); map.get(key).push(a); } const groups = [...map.entries()].map(([key, list]) => ({ key, items: list.slice().sort((x, y) => String(y.updatedAt || "").localeCompare(String(x.updatedAt || ""))), })); // Stable, friendly order: alphabetical by subject, "Other" last. groups.sort((a, b) => { if (a.key === "Other") return 1; if (b.key === "Other") return -1; return a.key.localeCompare(b.key); }); return groups; }