diff --git a/components/carnet/notes-view.tsx b/components/carnet/notes-view.tsx index 29d6a48..c2dd452 100644 --- a/components/carnet/notes-view.tsx +++ b/components/carnet/notes-view.tsx @@ -78,15 +78,20 @@ export const NotesView: React.FC = ({ return [...notes].sort((a, b) => { if (currentFolder === 'Diary' || currentFolder === 'Health') { // For Diary and Health, sort by date in title first - const dateA = a.title.match(/^(\d{4}-\d{2}-\d{2})/); - const dateB = b.title.match(/^(\d{4}-\d{2}-\d{2})/); + const dateA = a.title?.match(/^(\d{4}-\d{2}-\d{2})/); + const dateB = b.title?.match(/^(\d{4}-\d{2}-\d{2})/); if (dateA && dateB) { return new Date(dateB[1]).getTime() - new Date(dateA[1]).getTime(); } + // If one has a date and the other doesn't, prioritize the one with date + if (dateA && !dateB) return -1; + if (!dateA && dateB) return 1; } // For all folders, sort by lastModified date - return new Date(b.lastModified).getTime() - new Date(a.lastModified).getTime(); + const timeA = a.lastModified ? new Date(a.lastModified).getTime() : 0; + const timeB = b.lastModified ? new Date(b.lastModified).getTime() : 0; + return timeB - timeA; }); };