GnuCash
Contact   Instructions
Bug 797576 - xaccAccountOrder shouldn't sort account codes as base-36
Summary: xaccAccountOrder shouldn't sort account codes as base-36
Status: RESOLVED FIXED
Alias: None
Product: GnuCash
Classification: Unclassified
Component: Engine (show other bugs)
Version: git-maint
Hardware: PC Windows
: Normal trivial
Target Milestone: ---
Assignee: core
QA Contact: core
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-15 00:14 EST by Christopher Lam
Modified: 2020-01-15 06:06 EST (History)
7 users (show)

See Also:


Attachments

Description Christopher Lam 2020-01-15 00:14:09 EST
Copied over from bug 797573

Because of the base-36 sorting, trying to sort accounts becomes unpredictable.

Perhaps it's time to remove it?

-=-=-=-=-=-

The problem is in how account codes are used in the comparison: their integer values are sometimes ignored and sometimes not.

Consider accounts:

Name = A, Code = '20'
Name = B, Code = '20_'
Name = C, Code = 'a'
Name = D, Code = '11'

A < B, because '20' < '20_' lexicographically.
B < C, because '20_' < 'a' lexicographically.
C < D, because a (10) < 11 numerically.
D < A, because 11 < 20 numerically.

A possible fix (works for me):

diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp
index fcacf1862..194976dbd 100644
--- a/libgnucash/engine/Account.cpp
+++ b/libgnucash/engine/Account.cpp
@@ -2239,18 +2239,6 @@ xaccAccountOrder (const Account *aa, const Account *ab)
     da = priv_aa->accountCode;
     db = priv_ab->accountCode;
 
-    /* If accountCodes are both base 36 integers do an integer sort */
-    la = strtoul (da, &endptr, 36);
-    if ((*da != '\0') && (*endptr == '\0'))
-    {
-        lb = strtoul (db, &endptr, 36);
-        if ((*db != '\0') && (*endptr == '\0'))
-        {
-            if (la < lb) return -1;
-            if (la > lb) return +1;
-        }
-    }
-
     /* Otherwise do a string sort */
     result = g_strcmp0 (da, db);
     if (result)
Comment 1 John Ralls 2020-01-15 00:35:35 EST
OK with me.
Comment 2 Geert Janssens 2020-01-15 03:18:43 EST
Same here.
Comment 3 Christopher Lam 2020-01-15 06:06:23 EST
Done. Many thanks to yegor.

Note You need to log in before you can comment on or make changes to this bug.