From 9617d0dd81692c7d23e06a079019fc0422283ed9 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 14 Dec 2020 09:36:34 -0800 Subject: [PATCH] ;lib: multibalancereport sorting implementation notes (#1414, #1415) --- hledger-lib/Hledger/Reports/MultiBalanceReport.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 36b88787a..ac9bd7c95 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -434,19 +434,20 @@ generateMultiBalanceReport rspec@ReportSpec{rsOpts=ropts} j valuation colspans c report = reportPercent ropts $ PeriodicReport colspans sortedrows totalsrow -- | Build the report rows. --- -- One row per account, with account name info, row amounts, row total and row average. +-- Rows are unsorted. buildReportRows :: ReportOpts -> HashMap AccountName DisplayName -> HashMap AccountName (Map DateSpan Account) -> [MultiBalanceReportRow] -buildReportRows ropts displaynames = toList . HM.mapMaybeWithKey mkRow +buildReportRows ropts displaynames = + toList . HM.mapMaybeWithKey mkRow -- toList of HashMap's Foldable instance - does not sort consistently where mkRow name accts = do displayname <- HM.lookup name displaynames return $ PeriodicReportRow displayname rowbals rowtot rowavg where - rowbals = map balance $ toList accts + rowbals = map balance $ toList accts -- toList of Map's Foldable instance - does sort by key -- The total and average for the row. -- These are always simply the sum/average of the displayed row amounts. -- Total for a cumulative/historical report is always the last column. @@ -526,7 +527,7 @@ sortRows ropts j sortedaccounttree = sortAccountTreeByAmount (fromMaybe NormallyPositive $ normalbalance_ ropts) accounttreewithbals sortedanames = map aname $ drop 1 $ flattenAccounts sortedaccounttree - -- Sort the report rows, representing a flat account list, by row total. + -- Sort the report rows, representing a flat account list, by row total (and then account name). sortFlatMBRByAmount :: [MultiBalanceReportRow] -> [MultiBalanceReportRow] sortFlatMBRByAmount = case fromMaybe NormallyPositive $ normalbalance_ ropts of NormallyPositive -> sortOn (\r -> (Down $ amt r, prrFullName r))