Skip to content

Commit

Permalink
Merge pull request #39 from JProgrammer/stable-sort
Browse files Browse the repository at this point in the history
Use stable sort
  • Loading branch information
xoofx authored Jun 21, 2022
2 parents ff0296f + 4be6bb7 commit dba86c1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/Tomlyn.Tests/ModelTests/TomlTableModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,36 @@ public void TestPrimitiveOrder()
}
}

[Test]
public void TestOrdering()
{
var t = new Tomlyn.Model.TomlTable();
for (var i = 0; i < 17; ++i)
{
t.Add($"Test{i}", "Test");
}

Assert.AreEqual(
@"Test0 = ""Test""
Test1 = ""Test""
Test2 = ""Test""
Test3 = ""Test""
Test4 = ""Test""
Test5 = ""Test""
Test6 = ""Test""
Test7 = ""Test""
Test8 = ""Test""
Test9 = ""Test""
Test10 = ""Test""
Test11 = ""Test""
Test12 = ""Test""
Test13 = ""Test""
Test14 = ""Test""
Test15 = ""Test""
Test16 = ""Test""
", Toml.FromModel(t));
}

private static void AssertJson(string input, string expectedJson)
{
var syntax = Toml.Parse(input);
Expand Down
6 changes: 4 additions & 2 deletions src/Tomlyn/Model/ModelToTomlTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;
using Tomlyn.Helpers;
using Tomlyn.Model.Accessors;
using Tomlyn.Syntax;
Expand Down Expand Up @@ -199,7 +200,8 @@ private bool VisitObject(ObjectDynamicAccessor accessor, object currentObject, b
}

// Sort primitive first
properties.Sort((left, right) =>
properties = properties.OrderBy(_ => _,
Comparer<KeyValuePair<string,object>>.Create((left, right) =>
{
var leftValue = left.Value;
var rightValue = right.Value;
Expand All @@ -219,7 +221,7 @@ private bool VisitObject(ObjectDynamicAccessor accessor, object currentObject, b

// Otherwise don't change the order if we don't have primitives
return 0;
});
})).ToList();

// Probe inline for each key
// If we require a key to be inlined, inline the rest
Expand Down

0 comments on commit dba86c1

Please sign in to comment.