Programmatically re-order role weights? I want control over the order in which roles' columns are displayed on the Permissions page..

I want to add code to my module installation file so that, after newly-configured roles' yaml settings have been applied (they list the permissions for each new role), it displays all the roles in the order I want.

How do I do this?

Motivation

The roles' yaml settings are part of a module created by Features.
Enabling the module in the target Drupal 8 system adds the new roles in the wrong order.

It is not possible to include the weights of the anonymous; authenticated and Administrator roles in the feature / module's yaml files as this causes a conflict that prevents the module from being enabled. So the solution is to apply the weights programmatically.

[edit] I mistakenly thought that the order of the roles' columns related to an hierarchy / inheritance of permissions. Corrected by @Berdir, thanks. Nevertheless I still want to display the roles' columns in the way I choose, so I have edited this question to reflect this.
Role weight is only used for some lists in the backend. It has no effect on the permissions that a user has.

The permissions or a user are always the sum of all permissions of all roles that he has, you can not remove permissions with roles.

All "users" either have the anonymous (when they're not a user, basically) or the authenticated user role. You can give anonymous users more permissions than authenticated users. But you can never remove any permission granted by the authenticated user role through another role.

This doesn't answer your question but considering the above, I don't think  you are asking the right question. Whatever problem you are trying to solve exactly, setting role weights is not the answer ;) If this is not enough to solve your problem, then try explain the actual problem that you are trying to solve.

Edit:

Anyway, to actually set the role weight in e.g. hook_install(), you can do this:

$role = \Drupal\user\entity\Role::load('anonymous');
$role-&gt;setWeight(0);
$role-&gt;save();


Again, keep in mind that this has no effect on the permissions a user has, it just affects how the roles are listed on the role and permission administration pages.