Simplify account total values before comparison

This way, if two account values are BALANCE types containing only a
single AMOUNT, then it will do the sorting comparison of the amounts --
since otherwise balances are ignored for the purposes of sorting.
This commit is contained in:
John Wiegley 2009-02-19 21:57:17 -04:00
parent 7fb328707c
commit 2694335e54
3 changed files with 16 additions and 5 deletions

View file

@ -53,10 +53,12 @@ namespace {
sort_values.push_back(sort_value_t());
sort_values.back().inverted = inverted;
sort_values.back().value = expr_t(node).calc(*scope).reduced();
sort_values.back().value =
expr_t(node).calc(*scope).reduced().simplified();
if (sort_values.back().value.is_null())
throw calc_error("Could not determine sorting value based an expression");
throw_(calc_error,
"Could not determine sorting value based an expression");
}
}
}
@ -107,6 +109,9 @@ bool compare_items<account_t>::operator()(account_t * left, account_t * right)
rxdata.add_flags(ACCOUNT_EXT_SORT_CALC);
}
DEBUG("value.sort", "Comparing accounts " << left->fullname()
<< " <> " << right->fullname());
return sort_value_is_less_than(lxdata.sort_values, rxdata.sort_values);
}

View file

@ -177,6 +177,13 @@ void sorted_accounts_iterator::sort_accounts(account_t& account,
std::stable_sort(deque.begin(), deque.end(),
compare_items<account_t>(sort_cmp));
#if defined(DEBUG_ON)
if (SHOW_DEBUG("accounts.sorted")) {
foreach (account_t * account, deque)
DEBUG("accounts.sorted", "Account: " << account->fullname());
}
#endif
}
void sorted_accounts_iterator::push_all(account_t& account)

View file

@ -1422,13 +1422,12 @@ bool sort_value_is_less_than(const std::list<sort_value_t>& left_values,
std::list<sort_value_t>::const_iterator left_iter = left_values.begin();
std::list<sort_value_t>::const_iterator right_iter = right_values.begin();
while (left_iter != left_values.end() &&
right_iter != right_values.end()) {
while (left_iter != left_values.end() && right_iter != right_values.end()) {
// Don't even try to sort balance values
if (! (*left_iter).value.is_balance() &&
! (*right_iter).value.is_balance()) {
DEBUG("value.sort",
"Comparing " << (*left_iter).value << " < " << (*right_iter).value);
" Comparing " << (*left_iter).value << " < " << (*right_iter).value);
if ((*left_iter).value < (*right_iter).value) {
DEBUG("value.sort", " is less");
return ! (*left_iter).inverted;