Could you maybe try to order by user_group_id in the function UserGropuDAO::getByRoleId, so the function would look like:
> $params = array((int) $contextId, (int) $roleId);
if ($default) $params[] = 1; // true $result = $this->retrieveRange( 'SELECT * FROM user_groups WHERE context_id = ? AND role_id = ? ' . ($default?' AND is_default = ?':'') .' ORDER BY user_group_id', $params, $dbResultRange );
Maybe this will already solve your problem. In the mean time I would add a new loop that would go through all groups found and let you know…
Thanks!
Bozana