Fix alphabetical ordering single element map (#2259)

diff --git a/pkgs/yaml_edit/CHANGELOG.md b/pkgs/yaml_edit/CHANGELOG.md
index 9342e9f..8794b7b 100644
--- a/pkgs/yaml_edit/CHANGELOG.md
+++ b/pkgs/yaml_edit/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 2.2.3
+
+- Fix alphabetical ordering when inserting into a single-element maps.
+  ([#2258](https://github.com/dart-lang/tools/issues/2258))
+
 ## 2.2.2
 
 - Suppress warnings previously printed to `stdout` when parsing YAML internally.
diff --git a/pkgs/yaml_edit/lib/src/utils.dart b/pkgs/yaml_edit/lib/src/utils.dart
index ef85526..0e28ac1 100644
--- a/pkgs/yaml_edit/lib/src/utils.dart
+++ b/pkgs/yaml_edit/lib/src/utils.dart
@@ -148,11 +148,7 @@
 int getMapInsertionIndex(YamlMap map, Object newKey) {
   final keys = map.nodes.keys.map((k) => k.toString()).toList();
 
-  // We can't deduce ordering if list is empty, so then we just we just append
-  if (keys.length <= 1) {
-    return map.length;
-  }
-
+  // Detect if the keys are not already sorted, append new entry to the end
   for (var i = 1; i < keys.length; i++) {
     if (keys[i].compareTo(keys[i - 1]) < 0) {
       return map.length;
diff --git a/pkgs/yaml_edit/pubspec.yaml b/pkgs/yaml_edit/pubspec.yaml
index 00ff720..6162832 100644
--- a/pkgs/yaml_edit/pubspec.yaml
+++ b/pkgs/yaml_edit/pubspec.yaml
@@ -1,5 +1,5 @@
 name: yaml_edit
-version: 2.2.2
+version: 2.2.3
 description: >-
   A library for YAML manipulation with comment and whitespace preservation.
 repository: https://github.com/dart-lang/tools/tree/main/pkgs/yaml_edit
diff --git a/pkgs/yaml_edit/test/editor_test.dart b/pkgs/yaml_edit/test/editor_test.dart
index b0a0081..0c76da6 100644
--- a/pkgs/yaml_edit/test/editor_test.dart
+++ b/pkgs/yaml_edit/test/editor_test.dart
@@ -29,9 +29,11 @@
 
       expect(yamlEditor.edits, [
         SourceEdit(5, 5, " YAML Ain't Markup Language"),
-        SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
-        SourceEdit(0, 33, '')
+        SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
+        SourceEdit(32, 32, '')
       ]);
+      expect(
+          yamlEditor.toString(), equals('XML: Extensible Markup Language\n'));
     });
 
     test('that do not automatically update with internal list', () {
@@ -48,9 +50,10 @@
       expect(firstEdits, [SourceEdit(5, 5, " YAML Ain't Markup Language")]);
       expect(yamlEditor.edits, [
         SourceEdit(5, 5, " YAML Ain't Markup Language"),
-        SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
-        SourceEdit(0, 33, '')
+        SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
+        SourceEdit(32, 32, '')
       ]);
+      expect(yamlEditor.toString(), 'XML: Extensible Markup Language\n');
     });
   });
 }
diff --git a/pkgs/yaml_edit/test/testdata/output/allowed_characters_in_keys.golden b/pkgs/yaml_edit/test/testdata/output/allowed_characters_in_keys.golden
index 3fd7885..4cfa06f 100644
--- a/pkgs/yaml_edit/test/testdata/output/allowed_characters_in_keys.golden
+++ b/pkgs/yaml_edit/test/testdata/output/allowed_characters_in_keys.golden
@@ -1,26 +1,26 @@
 test: test
 ---
-test: test
 "a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
+test: test
 ---
-test: test
-"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
 ?foo: safe question mark
+"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
+test: test
 ---
-test: test
-"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
-?foo: safe question mark
 :foo: safe colon
----
-test: test
-"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
 ?foo: safe question mark
-:foo: safe colon
+"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
+test: test
+---
 -foo: safe dash
----
-test: test
-"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
-?foo: safe question mark
 :foo: safe colon
+?foo: safe question mark
+"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
+test: test
+---
 -foo: safe dash
+:foo: safe colon
+?foo: safe question mark
+"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
+test: test
 this is#not: a comment
diff --git a/pkgs/yaml_edit/test/update_test.dart b/pkgs/yaml_edit/test/update_test.dart
index 01dfde0..b52c640 100644
--- a/pkgs/yaml_edit/test/update_test.dart
+++ b/pkgs/yaml_edit/test/update_test.dart
@@ -715,12 +715,9 @@
         doc.update(['XML'], 'Extensible Markup Language');
 
         expect(
-          doc.toString(),
-          equals(
-            "{YAML: YAML Ain't Markup Language, "
-            'XML: Extensible Markup Language}',
-          ),
-        );
+            doc.toString(),
+            '{XML: Extensible Markup Language, '
+            "YAML: YAML Ain't Markup Language}");
         expectYamlBuilderValue(doc, {
           'XML': 'Extensible Markup Language',
           'YAML': "YAML Ain't Markup Language",
@@ -745,6 +742,31 @@
         expectYamlBuilderValue(doc, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
       });
 
+      test('Preserves alphabetical order single', () {
+        {
+          final doc = YamlEditor('''
+b: 2
+''');
+          doc.update(['a'], 1);
+          expect(doc.toString(), '''
+a: 1
+b: 2
+''');
+          expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
+        }
+        {
+          final doc = YamlEditor('''
+a: 1
+''');
+          doc.update(['b'], 2);
+          expect(doc.toString(), '''
+a: 1
+b: 2
+''');
+          expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
+        }
+      });
+
       // Regression testing to ensure it works without leading whitespace
       test('(2)', () {
         final doc = YamlEditor('a: 1');