From c1f54b46fb681f4ab503b9c536c35b6d8af0c939 Mon Sep 17 00:00:00 2001 From: zanaptak <53196138+zanaptak@users.noreply.github.com> Date: Thu, 30 Jul 2020 20:23:36 -0500 Subject: [PATCH 1/3] Avoid attribute swap during compare. Fixes #9. --- Program.cs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Program.cs b/Program.cs index 1179385..94445a1 100644 --- a/Program.cs +++ b/Program.cs @@ -187,30 +187,29 @@ static int SortDelegate( XmlNode a, XmlNode b ) // Sorting attributes, if specified, is done before node sorting happens.. if (result == 0) { - var col1 = (a.Attributes.Count >= b.Attributes.Count) ? a.Attributes : b.Attributes; - var col2 = (a.Attributes.Count >= b.Attributes.Count) ? b.Attributes : a.Attributes; - - for (var i = 0; i < col1.Count; i++) { - if (i < col2.Count) { - var aa = col1[i]; - var bb = col2[i]; - result = string.Compare(aa.Name, bb.Name, sort_attr_comp); - if (result == 0) { - result = string.Compare(aa.Value, bb.Value, sort_attr_comp); - if (result != 0) { - return result; - } - // Attribute name and value match.. continue loop. - } else { + var sharedCount = Math.Min(a.Attributes.Count, b.Attributes.Count); + + for (var i = 0; i < sharedCount; i++) { + var aa = a.Attributes[i]; + var bb = b.Attributes[i]; + result = string.Compare(aa.Name, bb.Name, sort_attr_comp); + if (result == 0) { + result = string.Compare(aa.Value, bb.Value, sort_attr_comp); + if (result != 0) { return result; } + // Attribute name and value match.. continue loop. } else { - return 1; + return result; } } - // If we get here, that means that the node's attributes (and values) all match.. - // TODO: Should we go down into the child node collections for sorting? + // If we get here, that means that the node's attributes (and values) all match, up to lesser count.. + + // Compare by count (meaing node with fewer attributes will sort first) + return a.Attributes.Count.CompareTo(b.Attributes.Count); + + // TODO: Should we go down into the child node collections for sorting, if equal attribute count? // See example `c.xml`.. //Console.WriteLine(a.Name + "==" + b.Name + " all attributes matched"); } From d4d21e17c01c4e0617f79ff85db4f1e660050727 Mon Sep 17 00:00:00 2001 From: zanaptak <53196138+zanaptak@users.noreply.github.com> Date: Mon, 3 Aug 2020 17:39:15 -0500 Subject: [PATCH 2/3] Handle null attribute list. Fixes #7. --- Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index 94445a1..de74276 100644 --- a/Program.cs +++ b/Program.cs @@ -187,7 +187,9 @@ static int SortDelegate( XmlNode a, XmlNode b ) // Sorting attributes, if specified, is done before node sorting happens.. if (result == 0) { - var sharedCount = Math.Min(a.Attributes.Count, b.Attributes.Count); + var aCount = a.Attributes?.Count ?? 0; + var bCount = b.Attributes?.Count ?? 0; + var sharedCount = Math.Min(aCount, bCount); for (var i = 0; i < sharedCount; i++) { var aa = a.Attributes[i]; @@ -207,7 +209,7 @@ static int SortDelegate( XmlNode a, XmlNode b ) // If we get here, that means that the node's attributes (and values) all match, up to lesser count.. // Compare by count (meaing node with fewer attributes will sort first) - return a.Attributes.Count.CompareTo(b.Attributes.Count); + return aCount.CompareTo(bCount); // TODO: Should we go down into the child node collections for sorting, if equal attribute count? // See example `c.xml`.. From 981ad0910ee39e76a13a1798ee800662a81e5f54 Mon Sep 17 00:00:00 2001 From: zanaptak <53196138+zanaptak@users.noreply.github.com> Date: Mon, 3 Aug 2020 17:55:53 -0500 Subject: [PATCH 3/3] Avoid initial reversing of node list. Fixes #4. --- Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index de74276..f110c0a 100644 --- a/Program.cs +++ b/Program.cs @@ -166,7 +166,7 @@ static void SortNodes( XmlNode node ) var nodes = new List(node.ChildNodes.Count); for (var i = node.ChildNodes.Count - 1; i >= 0; i--) { - nodes.Add(node.ChildNodes[i]); + nodes.Insert(0, node.ChildNodes[i]); node.RemoveChild(node.ChildNodes[i]); }