In Magento there is a functionality where you can define the order of total calculation by specifing before and after which totals a total should be run.
I added a c
EDIT: This answer is wrong. See the discussion in the comments.
As Vinai noted, the problem is that the order function returns 0 even if the parameters are not equal. I modified the function to fall back on the string order of the keys as follows:
protected function _compareTotals($a, $b)
{
$aCode = $a['_code'];
$bCode = $b['_code'];
if (in_array($aCode, $b['after']) || in_array($bCode, $a['before'])) {
$res = -1;
} elseif (in_array($bCode, $a['after']) || in_array($aCode, $b['before'])) {
$res = 1;
} else {
$res = strcmp($aCode, $bCode); // was $res = 0 before
}
return $res;
}