diff --git a/core/modules/user/src/Entity/Role.php b/core/modules/user/src/Entity/Role.php
index 5ecbc02..a0b709c 100644
--- a/core/modules/user/src/Entity/Role.php
+++ b/core/modules/user/src/Entity/Role.php
@@ -178,6 +178,12 @@ public function preSave(EntityStorageInterface $storage) {
       });
       $this->weight = $max + 1;
     }
+
+    if (!$this->isSyncing()) {
+      // Permissions are always ordered alphabetically to avoid conflicts in the
+      // exported configuration.
+      sort($this->permissions);
+    }
   }
 
 }
diff --git a/core/modules/user/src/Plugin/views/field/Permissions.php b/core/modules/user/src/Plugin/views/field/Permissions.php
index bd5c11d..c9d5a9f 100644
--- a/core/modules/user/src/Plugin/views/field/Permissions.php
+++ b/core/modules/user/src/Plugin/views/field/Permissions.php
@@ -75,7 +75,6 @@ public function query() {
   }
 
   public function preRender(&$values) {
-    $uids = array();
     $this->items = array();
 
     $permission_names = \Drupal::service('user.permissions')->getPermissions();
@@ -100,10 +99,8 @@ public function preRender(&$values) {
         }
       }
 
-      foreach ($uids as $uid) {
-        if (isset($this->items[$uid])) {
-          ksort($this->items[$uid]);
-        }
+      foreach ($this->items as &$permission) {
+        ksort($permission);
       }
     }
   }
diff --git a/core/modules/user/tests/src/Kernel/UserRoleEntityTest.php b/core/modules/user/tests/src/Kernel/UserRoleEntityTest.php
new file mode 100644
index 0000000..562ada1
--- /dev/null
+++ b/core/modules/user/tests/src/Kernel/UserRoleEntityTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Drupal\Tests\user\Kernel;
+
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\user\Entity\Role;
+
+/**
+ * @group user
+ */
+class UserRoleEntityTest extends KernelTestBase {
+
+  public static $modules = ['system', 'user'];
+
+  public function testOrderOfPermissions() {
+    $role = Role::create(['id' => 'test_role']);
+    $role->grantPermission('b')
+      ->grantPermission('a')
+      ->grantPermission('c')
+      ->save();
+    $this->assertEquals($role->getPermissions(), ['a', 'b', 'c']);
+
+    $role->revokePermission('b')->save();
+    $this->assertEquals($role->getPermissions(), ['a', 'c']);
+
+    $role->grantPermission('b')->save();
+    $this->assertEquals($role->getPermissions(), ['a', 'b', 'c']);
+  }
+
+}
diff --git a/core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php b/core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php
index 9def2b8..c0c791f 100644
--- a/core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php
+++ b/core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php
@@ -37,9 +37,9 @@ public function testFieldPermission() {
     $expected_permissions[$this->users[2]->id()][] = t('Administer permissions');
     // View user profiles comes first, because we sort by the permission
     // machine name.
+    $expected_permissions[$this->users[3]->id()][] = t('View user information');
     $expected_permissions[$this->users[3]->id()][] = t('Administer permissions');
     $expected_permissions[$this->users[3]->id()][] = t('Administer users');
-    $expected_permissions[$this->users[3]->id()][] = t('View user information');
 
     foreach ($view->result as $index => $row) {
       $uid = $view->field['uid']->getValue($row);
