From 1708f0b441d55ec81a80b30ae84861926053f745 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 2 Dec 2014 11:16:51 -0800 Subject: [PATCH] csv: try to preserve order of same-day transactions If the CSV records appear to have been in reverse date order, we'll now reverse them all before also sorting by transaction date, so that the original order of same-day transactions is preserved. We detect this using a simple heuristic: if the first converted transaction's date is later than the last's. --- hledger-lib/Hledger/Read/CsvReader.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index efcc6df72..18fc8d73a 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -121,7 +121,13 @@ readJournalFromCsv mrulesfile csvfile csvdata = let txns = snd $ mapAccumL (\pos r -> (pos, transactionFromCsvRecord (incSourceLine pos 1) rules r)) (initialPos parsecfilename) records - return $ Right nulljournal{jtxns=sortBy (comparing tdate) txns} + + -- heuristic: if the records appear to have been in reverse date order, + -- reverse them all as well as doing a txn date sort, + -- so that same-day txns' original order is preserved + txns' | length txns > 1 && tdate (head txns) > tdate (last txns) = reverse txns + | otherwise = txns + return $ Right nulljournal{jtxns=sortBy (comparing tdate) txns'} parseCsv :: FilePath -> String -> IO (Either ParseError CSV) parseCsv path csvdata =