From db5d2a05c000b8fbc6ca6cc0e5f07186db5f3a8b Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Thu, 26 Feb 2015 23:21:44 -0500 Subject: [PATCH] Change string compare to natural compare --- NBTExplorer.Installer/Product.wxs | 2 +- NBTExplorer/Controllers/NodeTreeController.cs | 146 +++++++++--------- NBTExplorer/Properties/AssemblyInfo.cs | 4 +- NBTModel/NBTModel.csproj | 1 + NBTModel/Properties/AssemblyInfo.cs | 4 +- NBTModel/Utility/NaturalComparer.cs | 70 +++++++++ 6 files changed, 150 insertions(+), 77 deletions(-) create mode 100644 NBTModel/Utility/NaturalComparer.cs diff --git a/NBTExplorer.Installer/Product.wxs b/NBTExplorer.Installer/Product.wxs index e906838..ccc20e8 100644 --- a/NBTExplorer.Installer/Product.wxs +++ b/NBTExplorer.Installer/Product.wxs @@ -3,7 +3,7 @@ diff --git a/NBTExplorer/Controllers/NodeTreeController.cs b/NBTExplorer/Controllers/NodeTreeController.cs index 7173a03..a7a599d 100644 --- a/NBTExplorer/Controllers/NodeTreeController.cs +++ b/NBTExplorer/Controllers/NodeTreeController.cs @@ -5,8 +5,9 @@ using System.Windows.Forms; using NBTExplorer.Model; using NBTExplorer.Vendor.MultiSelectTreeView; using NBTExplorer.Windows; -using Substrate.Nbt; +using Substrate.Nbt; using System.Collections; +using NBTExplorer.Utility; namespace NBTExplorer.Controllers { @@ -19,76 +20,77 @@ namespace NBTExplorer.Controllers { Message = message; } - } - - public class NodeTreeComparer : IComparer - { - public int OrderForTag(TagType tagID) - { - switch (tagID) - { - case TagType.TAG_COMPOUND: - return 0; - case TagType.TAG_LIST: - return 1; - case TagType.TAG_BYTE: - case TagType.TAG_SHORT: - case TagType.TAG_INT: - case TagType.TAG_LONG: - case TagType.TAG_FLOAT: - case TagType.TAG_DOUBLE: - case TagType.TAG_STRING: - return 2; - default: - return 3; - } - } - - public int OrderForNode(object node) - { - if (node is DirectoryDataNode) - { - return 0; - } - else - { - return 1; - } - } - - public int Compare(object x, object y) - { - TreeNode tx = x as TreeNode; - TreeNode ty = y as TreeNode; - TagDataNode dx = tx.Tag as TagDataNode; - TagDataNode dy = ty.Tag as TagDataNode; - - if (dx == null || dy == null) - { - int nodeOrder = this.OrderForNode(tx.Tag).CompareTo(this.OrderForNode(ty.Tag)); - if (nodeOrder != 0) - { - return nodeOrder; - } - else - { - return tx.Text.CompareTo(ty.Text); - } - } - - TagType idx = dx.Tag.GetTagType(); - TagType idy = dy.Tag.GetTagType(); - int tagOrder = this.OrderForTag(idx).CompareTo(this.OrderForTag(idy)); - if (tagOrder != 0) - { - return tagOrder; - } - else - { - return dx.NodeDisplay.CompareTo(dy.NodeDisplay); - } - - } + } + + public class NodeTreeComparer : IComparer + { + private NaturalComparer _comparer = new NaturalComparer(); + + public int OrderForTag(TagType tagID) + { + switch (tagID) + { + case TagType.TAG_COMPOUND: + return 0; + case TagType.TAG_LIST: + return 1; + case TagType.TAG_BYTE: + case TagType.TAG_SHORT: + case TagType.TAG_INT: + case TagType.TAG_LONG: + case TagType.TAG_FLOAT: + case TagType.TAG_DOUBLE: + case TagType.TAG_STRING: + return 2; + default: + return 3; + } + } + + public int OrderForNode(object node) + { + if (node is DirectoryDataNode) + { + return 0; + } + else + { + return 1; + } + } + + public int Compare(object x, object y) + { + TreeNode tx = x as TreeNode; + TreeNode ty = y as TreeNode; + TagDataNode dx = tx.Tag as TagDataNode; + TagDataNode dy = ty.Tag as TagDataNode; + + if (dx == null || dy == null) + { + int nodeOrder = this.OrderForNode(tx.Tag).CompareTo(this.OrderForNode(ty.Tag)); + if (nodeOrder != 0) + { + return nodeOrder; + } + else + { + return _comparer.Compare(tx.Text, ty.Text); + } + } + + TagType idx = dx.Tag.GetTagType(); + TagType idy = dy.Tag.GetTagType(); + int tagOrder = this.OrderForTag(idx).CompareTo(this.OrderForTag(idy)); + if (tagOrder != 0) + { + return tagOrder; + } + else + { + return _comparer.Compare(dx.NodeDisplay, dy.NodeDisplay); + } + } } public class NodeTreeController @@ -103,7 +105,7 @@ namespace NBTExplorer.Controllers public NodeTreeController (TreeView nodeTree) { - _nodeTree = nodeTree; + _nodeTree = nodeTree; nodeTree.TreeViewNodeSorter = new NodeTreeComparer(); _multiTree = nodeTree as MultiSelectTreeView; diff --git a/NBTExplorer/Properties/AssemblyInfo.cs b/NBTExplorer/Properties/AssemblyInfo.cs index 9685374..697c053 100644 --- a/NBTExplorer/Properties/AssemblyInfo.cs +++ b/NBTExplorer/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.7.5.0")] -[assembly: AssemblyFileVersion("2.7.5.0")] +[assembly: AssemblyVersion("2.7.6.0")] +[assembly: AssemblyFileVersion("2.7.6.0")] diff --git a/NBTModel/NBTModel.csproj b/NBTModel/NBTModel.csproj index f1f6341..f48a8b4 100644 --- a/NBTModel/NBTModel.csproj +++ b/NBTModel/NBTModel.csproj @@ -72,6 +72,7 @@ + diff --git a/NBTModel/Properties/AssemblyInfo.cs b/NBTModel/Properties/AssemblyInfo.cs index 8c803aa..45d71d1 100644 --- a/NBTModel/Properties/AssemblyInfo.cs +++ b/NBTModel/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.3.0")] -[assembly: AssemblyFileVersion("1.0.3.0")] +[assembly: AssemblyVersion("1.0.4.0")] +[assembly: AssemblyFileVersion("1.0.4.0")] diff --git a/NBTModel/Utility/NaturalComparer.cs b/NBTModel/Utility/NaturalComparer.cs new file mode 100644 index 0000000..e59f7f6 --- /dev/null +++ b/NBTModel/Utility/NaturalComparer.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace NBTExplorer.Utility +{ + // NaturalComparer implementation by Justin.Jones + // Licensed under The Code Project Open License (CPOL) (http://www.codeproject.com/info/cpol10.aspx) + + public class NaturalComparer : Comparer, IDisposable + { + private Dictionary table; + + public NaturalComparer () + { + table = new Dictionary(); + } + + public void Dispose () + { + table.Clear(); + table = null; + } + + public override int Compare (string x, string y) + { + if (x == y) { + return 0; + } + string[] x1, y1; + if (!table.TryGetValue(x, out x1)) { + x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); + table.Add(x, x1); + } + if (!table.TryGetValue(y, out y1)) { + y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); + table.Add(y, y1); + } + + for (int i = 0; i < x1.Length && i < y1.Length; i++) { + if (x1[i] != y1[i]) { + return PartCompare(x1[i], y1[i]); + } + } + if (y1.Length > x1.Length) { + return 1; + } + else if (x1.Length > y1.Length) { + return -1; + } + else { + return 0; + } + } + + private static int PartCompare (string left, string right) + { + int x, y; + if (!int.TryParse(left, out x)) { + return left.CompareTo(right); + } + + if (!int.TryParse(right, out y)) { + return left.CompareTo(right); + } + + return x.CompareTo(y); + } + } +}