gnucash stable: [Transaction.cpp] use g_list_sort to partition splits
Christopher Lam
clam at code.gnucash.org
Sun Sep 6 11:53:34 EDT 2026
Updated via https://github.com/Gnucash/gnucash/commit/00cc11fb (commit)
from https://github.com/Gnucash/gnucash/commit/17f1d581 (commit)
commit 00cc11fb052663bcd97a14de7bea11ef7052d3df
Author: Christopher Lam <christopher.lck at gmail.com>
Date: Sun Sep 6 23:32:41 2026 +0800
[Transaction.cpp] use g_list_sort to partition splits
because g_list_sort is a stable sort, and can partition trans->splits
into neg < !neg efficiently
diff --git a/libgnucash/engine/Transaction.cpp b/libgnucash/engine/Transaction.cpp
index 56d3c59dfa..b5402fccdc 100644
--- a/libgnucash/engine/Transaction.cpp
+++ b/libgnucash/engine/Transaction.cpp
@@ -537,33 +537,19 @@ xaccTransDump (const Transaction *trans, const char *tag)
}
#endif
+static int
+split_sign_cmp (gconstpointer a, gconstpointer b)
+{
+ bool a_neg = gnc_numeric_negative_p (xaccSplitGetValue (GNC_SPLIT (a)));
+ bool b_neg = gnc_numeric_negative_p (xaccSplitGetValue (GNC_SPLIT (b)));
+ return a_neg == b_neg ? 0 : a_neg ? 1 : -1;
+}
+
void
xaccTransSortSplits (Transaction *trans)
{
- GList *node, *new_list = nullptr;
- Split *split;
-
- /* first debits */
- for (node = trans->splits; node; node = node->next)
- {
- split = GNC_SPLIT(node->data);
- if (gnc_numeric_negative_p (xaccSplitGetValue(split)))
- continue;
- new_list = g_list_prepend (new_list, split);
- }
-
- /* then credits */
- for (node = trans->splits; node; node = node->next)
- {
- split = GNC_SPLIT(node->data);
- if (!gnc_numeric_negative_p (xaccSplitGetValue(split)))
- continue;
- new_list = g_list_prepend (new_list, split);
- }
-
- /* install newly sorted list */
- g_list_free(trans->splits);
- trans->splits = g_list_reverse (new_list);
+ g_return_if_fail (trans);
+ trans->splits = g_list_sort (trans->splits, split_sign_cmp);
}
Summary of changes:
libgnucash/engine/Transaction.cpp | 34 ++++++++++------------------------
1 file changed, 10 insertions(+), 24 deletions(-)
More information about the gnucash-changes
mailing list