diff options
author | John Wiegley <johnw@newartisans.com> | 2004-08-19 03:06:11 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2004-08-19 03:06:11 -0400 |
commit | 965e1fc28f725e830a6f51a5d41e7a3850d15b12 (patch) | |
tree | 95613fa61819f0b39308c1e49a31459f66d69965 | |
parent | 61cc6c5a9a7f1439ed96d362176ab8b54bf5a8ed (diff) | |
download | fork-ledger-965e1fc28f725e830a6f51a5d41e7a3850d15b12.tar.gz fork-ledger-965e1fc28f725e830a6f51a5d41e7a3850d15b12.tar.bz2 fork-ledger-965e1fc28f725e830a6f51a5d41e7a3850d15b12.zip |
always print balance amounts sorted alphabetically by commodity
-rw-r--r-- | balance.cc | 45 |
1 files changed, 31 insertions, 14 deletions
@@ -1,5 +1,7 @@ #include "ledger.h" +#include <deque> + namespace ledger { amount_t balance_t::amount(const commodity_t * commodity) const @@ -30,6 +32,12 @@ balance_t balance_t::value(const std::time_t moment) const return temp; } +struct compare_amount_commodities { + bool operator()(const amount_t * left, const amount_t * right) const { + return left->commodity->symbol < right->commodity->symbol; + } +}; + void balance_t::write(std::ostream& out, const int first_width, const int latter_width) const @@ -40,24 +48,33 @@ void balance_t::write(std::ostream& out, if (lwidth == -1) lwidth = first_width; + typedef std::deque<const amount_t *> amounts_deque; + + amounts_deque sorted; for (amounts_map::const_iterator i = amounts.begin(); i != amounts.end(); + i++) + if ((*i).second) + sorted.push_back(&(*i).second); + + std::stable_sort(sorted.begin(), sorted.end(), + compare_amount_commodities()); + + for (amounts_deque::const_iterator i = sorted.begin(); + i != sorted.end(); i++) { - if ((*i).second) { - int width; - - if (! first) { - out << std::endl; - width = lwidth; - } else { - first = false; - width = first_width; - } - - out.width(width); - out.fill(' '); - out << std::right << std::string((*i).second); + int width; + if (! first) { + out << std::endl; + width = lwidth; + } else { + first = false; + width = first_width; } + + out.width(width); + out.fill(' '); + out << std::right << std::string(**i); } if (first) { |