Yes, reports are "theoretically" arranged by class_id. Theoretically because today reports are much different, at least in Slovenia (a sort of GIFI system, where many accounts are merged together in certain category, not as in COA or in classic BS or PL reports...). So, I consider class_id only as a tool to decide how to record a transaction (debit / credit). In any case, in account management screens I like things to be listed as prescribed by the COA. In any case, I've found the file and changed 1 line, and I hope we are both (all) happy... ;-)
About my issue (so that 13 is not less than 2), it can be solved.
Try the following query:
SELECT c5.id, c5.name, c5.class_id, c5.parent, c5.inactive FROM (
SELECT IF(c4.parent3 = '' OR c4.parent3 = '-1', c4.id3, c4.parent3) AS id4,
IF(c4.parent3 = '' OR c4.parent3 = '-1', '-1', (SELECT parent FROM 0_chart_types cx4 WHERE cx4.id = c4.parent3)) AS parent4,
c4.* FROM (
SELECT IF(c3.parent2 = '' OR c3.parent2 = '-1', c3.id2, c3.parent2) AS id3,
IF(c3.parent2 = '' OR c3.parent2 = '-1', '-1', (SELECT parent FROM 0_chart_types cx3 WHERE cx3.id = c3.parent2)) AS parent3,
c3.* FROM (
SELECT IF(c2.parent1 = '' OR c2.parent1 = '-1', c2.id1, c2.parent1) AS id2,
IF(c2.parent1 = '' OR c2.parent1 = '-1', '-1', (SELECT parent FROM 0_chart_types cx2 WHERE cx2.id = c2.parent1)) AS parent2,
c2.* FROM (
SELECT IF(c1.parent = '' OR c1.parent = '-1', c1.id, c1.parent) AS id1,
IF(c1.parent = '' OR c1.parent = '-1', '-1', (SELECT parent FROM 0_chart_types cx1 WHERE cx1.id = c1.parent)) AS parent1,
c1.* FROM (
SELECT * FROM 0_chart_types c) AS c1) AS c2) AS c3) AS c4
) AS c5 ORDER BY
CAST(id4 AS UNSIGNED), CAST(id3 AS UNSIGNED), CAST(id2 AS UNSIGNED), CAST(id1 AS UNSIGNED), id, parent;
I think so many recursive levels should be enough to sort 5 levels of account groups properly.
I've tested that query with account group table, it seems to be OK (a table in account group add/edit form is created well; haven't tested the situation where $all, $class_id or $parent would be false though...). Where is the query which creates the dropdown list for the "Subgroup Of:" field (I'd like to fix that one too...)?
You have to add your class_id to the sort, of course... ;-)