Repository: symfony/flex (php)

Issue:
Respect order when adding package to package.json
I installed a bundle using the recent feature of Symfony UX, and now I see a new package in `devDependencies` section of my package.json file.
It would be possible to get such new package added by respecting existing alphabetical order?

Example of what I get now:
```json
{
  "dependencies": {
    "bootstrap-sass": "^3.4.1",
    "jquery": "^3.5.1"
  },
  "devDependencies": {
    "@symfony/webpack-encore": "^0.33.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^9.0.1",
    "@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets"
  }
}
```

What I would like to get instead: 

```json
{
  "dependencies": {
    "bootstrap-sass": "^3.4.1",
    "jquery": "^3.5.1"
  },
  "devDependencies": {
    "@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets",
    "@symfony/webpack-encore": "^0.33.0",
    "node-sass": "^4.14.1",
    "sass-loader": "^9.0.1"
  }
}
```

Pull request:
fix package sorting
This should fix #726

Patch:
diff --git a/src/PackageJsonSynchronizer.php b/src/PackageJsonSynchronizer.php
index 87d91a3..8ceba37 100644
--- a/src/PackageJsonSynchronizer.php
+++ b/src/PackageJsonSynchronizer.php
@@ -63,6 +63,15 @@ class PackageJsonSynchronizer
         }
     }
 
+    private static function sortPackages(JsonManipulator $manipulator, $subNode)
+    {
+        $content = json_decode($manipulator->getContents(), true);
+        $subNodeContent = $content[$subNode];
+        ksort($subNodeContent);
+
+        return json_encode([$subNode => $subNodeContent]);
+    }
+
     private function addPackageJsonLink(string $phpPackage)
     {
         if (!$assetsDir = $this->resolveAssetsDir($phpPackage)) {
@@ -71,7 +80,7 @@ class PackageJsonSynchronizer
 
         $manipulator = new JsonManipulator(file_get_contents($this->rootDir.'/package.json'));
         $manipulator->addSubNode('devDependencies', '@'.$phpPackage, 'file:vendor/'.$phpPackage.$assetsDir);
-        file_put_contents($this->rootDir.'/package.json', $manipulator->getContents());
+        file_put_contents($this->rootDir.'/package.json', self::sortPackages($manipulator, 'devDependencies'));
     }
 
     private function registerWebpackResources(array $phpPackages)


Test patch:
diff --git a/tests/PackageJsonSynchronizerTest.php b/tests/PackageJsonSynchronizerTest.php
index 7b1566b..bb9b038 100644
--- a/tests/PackageJsonSynchronizerTest.php
+++ b/tests/PackageJsonSynchronizerTest.php
@@ -65,9 +65,9 @@ class PackageJsonSynchronizerTest extends TestCase
         $this->assertSame(
             [
                 'devDependencies' => [
+                    '@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
                     '@symfony/stimulus-bridge' => '^1.0.0',
                     'stimulus' => '^1.1.1',
-                    '@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
                 ],
             ],
             json_decode(file_get_contents($this->tempDir.'/package.json'), true)
@@ -101,10 +101,10 @@ class PackageJsonSynchronizerTest extends TestCase
         $this->assertSame(
             [
                 'devDependencies' => [
-                    '@symfony/stimulus-bridge' => '^1.0.0',
-                    'stimulus' => '^1.1.1',
                     '@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
                     '@symfony/new-package' => 'file:vendor/symfony/new-package/assets',
+                    '@symfony/stimulus-bridge' => '^1.0.0',
+                    'stimulus' => '^1.1.1',
                 ],
             ],
             json_decode(file_get_contents($this->tempDir.'/package.json'), true)