From 92645cca148cf33757d479013ebe0cdd1633c65d Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 14 Dec 2020 17:41:19 +1100 Subject: [PATCH] lib: Sorting accounts by amount should also consider account name after amounts. --- hledger-lib/Hledger/Data/Account.hs | 5 +++-- hledger-lib/Hledger/Reports/MultiBalanceReport.hs | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hledger-lib/Hledger/Data/Account.hs b/hledger-lib/Hledger/Data/Account.hs index 597d8a085..43a29bddb 100644 --- a/hledger-lib/Hledger/Data/Account.hs +++ b/hledger-lib/Hledger/Data/Account.hs @@ -203,8 +203,9 @@ sortAccountTreeByAmount :: NormalSign -> Account -> Account sortAccountTreeByAmount normalsign = mapAccounts $ \a -> a{asubs=sortSubs $ asubs a} where sortSubs = case normalsign of - NormallyPositive -> sortOn (Down . normaliseMixedAmountSquashPricesForDisplay . aibalance) - NormallyNegative -> sortOn ( normaliseMixedAmountSquashPricesForDisplay . aibalance) + NormallyPositive -> sortOn (\a -> (Down $ amt a, aname a)) + NormallyNegative -> sortOn (\a -> (amt a, aname a)) + amt = normaliseMixedAmountSquashPricesForDisplay . aibalance -- | Add extra info for this account derived from the Journal's -- account directives, if any (comment, tags, declaration order..). diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index cfbb17b52..36b88787a 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -528,9 +528,9 @@ sortRows ropts j -- Sort the report rows, representing a flat account list, by row total. sortFlatMBRByAmount :: [MultiBalanceReportRow] -> [MultiBalanceReportRow] - sortFlatMBRByAmount = case normalbalance_ ropts of - Just NormallyNegative -> sortOn amt - _ -> sortOn (Down . amt) + sortFlatMBRByAmount = case fromMaybe NormallyPositive $ normalbalance_ ropts of + NormallyPositive -> sortOn (\r -> (Down $ amt r, prrFullName r)) + NormallyNegative -> sortOn (\r -> (amt r, prrFullName r)) where amt = normaliseMixedAmountSquashPricesForDisplay . prrTotal -- Sort the report rows by account declaration order then account name.