Programmatically re-order role weights?
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.