diff --git a/src/Roux/Server.hs b/src/Roux/Server.hs index 6aa07e2..5e9bf8e 100644 --- a/src/Roux/Server.hs +++ b/src/Roux/Server.hs @@ -759,13 +759,16 @@ parseDate t = case T.splitOn "-" t of fromGregorianValid y' m' d' _ -> Nothing --- | Group entries by month label (e.g. "March 2026"). +-- | Group entries by month label (e.g. "March 2026"), sorted newest-first. groupEntries :: [CookLog.CookEntry] -> [(Text, [CookLog.CookEntry])] groupEntries [] = [] groupEntries entries = let monthKey d = T.pack $ Time.formatTime Time.defaultTimeLocale "%B %Y" d groups = Map.fromListWith (++) [(monthKey (CookLog.ceCookedDate e), [e]) | e <- entries] - sortedMonths = reverse (Map.keys groups) + -- Sort month groups by the newest entry date in each group, descending. + -- Every group is non-empty by construction. + monthDate = foldl max (toEnum 0) . map CookLog.ceCookedDate + sortedMonths = map fst $ sortOn (Down . monthDate . snd) $ Map.toList groups in [(m, Map.findWithDefault [] m groups) | m <- sortedMonths] -- | Look up a recipe by filename (case-insensitive, .cook extension optional).