always print balance amounts sorted alphabetically by commodity
This commit is contained in:
parent
61cc6c5a9a
commit
965e1fc28f
1 changed files with 31 additions and 14 deletions
45
balance.cc
45
balance.cc
|
|
@ -1,5 +1,7 @@
|
||||||
#include "ledger.h"
|
#include "ledger.h"
|
||||||
|
|
||||||
|
#include <deque>
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
amount_t balance_t::amount(const commodity_t * commodity) const
|
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;
|
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,
|
void balance_t::write(std::ostream& out,
|
||||||
const int first_width,
|
const int first_width,
|
||||||
const int latter_width) const
|
const int latter_width) const
|
||||||
|
|
@ -40,24 +48,33 @@ void balance_t::write(std::ostream& out,
|
||||||
if (lwidth == -1)
|
if (lwidth == -1)
|
||||||
lwidth = first_width;
|
lwidth = first_width;
|
||||||
|
|
||||||
|
typedef std::deque<const amount_t *> amounts_deque;
|
||||||
|
|
||||||
|
amounts_deque sorted;
|
||||||
for (amounts_map::const_iterator i = amounts.begin();
|
for (amounts_map::const_iterator i = amounts.begin();
|
||||||
i != amounts.end();
|
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++) {
|
i++) {
|
||||||
if ((*i).second) {
|
int width;
|
||||||
int width;
|
if (! first) {
|
||||||
|
out << std::endl;
|
||||||
if (! first) {
|
width = lwidth;
|
||||||
out << std::endl;
|
} else {
|
||||||
width = lwidth;
|
first = false;
|
||||||
} else {
|
width = first_width;
|
||||||
first = false;
|
|
||||||
width = first_width;
|
|
||||||
}
|
|
||||||
|
|
||||||
out.width(width);
|
|
||||||
out.fill(' ');
|
|
||||||
out << std::right << std::string((*i).second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out.width(width);
|
||||||
|
out.fill(' ');
|
||||||
|
out << std::right << std::string(**i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (first) {
|
if (first) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue