diff --git a/CodeGen/Generators/QuantityRelationsParser.cs b/CodeGen/Generators/QuantityRelationsParser.cs index 00664b036d..9352cd6334 100644 --- a/CodeGen/Generators/QuantityRelationsParser.cs +++ b/CodeGen/Generators/QuantityRelationsParser.cs @@ -60,6 +60,9 @@ public static void ParseAndApplyRelations(string rootDir, Quantity[] quantities) .Select(r => r with { RightQuantity = timeSpanQuantity }) .ToList()); + // Sort all relations to keep generated operators in a consistent order. + relations.Sort(); + foreach (var quantity in quantities) { var quantityRelations = new List(); diff --git a/CodeGen/JsonTypes/QuantityRelation.cs b/CodeGen/JsonTypes/QuantityRelation.cs index fa7c73056c..fa9c4199b3 100644 --- a/CodeGen/JsonTypes/QuantityRelation.cs +++ b/CodeGen/JsonTypes/QuantityRelation.cs @@ -1,9 +1,11 @@ // Licensed under MIT No Attribution, see LICENSE file at the root. // Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. +using System; + namespace CodeGen.JsonTypes { - internal record QuantityRelation + internal record QuantityRelation : IComparable { public string Operator = null!; @@ -15,5 +17,18 @@ internal record QuantityRelation public Quantity ResultQuantity = null!; public Unit ResultUnit = null!; + + private string SortString => ResultQuantity.Name + + ResultUnit.SingularName + + LeftQuantity.Name + + LeftUnit.SingularName + + Operator + + RightQuantity.Name + + RightUnit.SingularName; + + public int CompareTo(QuantityRelation? other) + { + return string.Compare(SortString, other?.SortString, StringComparison.Ordinal); + } } } \ No newline at end of file