Change string compare to natural compare

This commit is contained in:
Justin Aquadro 2015-02-26 23:21:44 -05:00
parent 5c32832281
commit db5d2a05c0
6 changed files with 150 additions and 77 deletions

View file

@ -3,7 +3,7 @@
<Product Id="*" <Product Id="*"
Name="NBTExplorer" Name="NBTExplorer"
Language="1033" Language="1033"
Version="2.7.5.0" Version="2.7.6.0"
Manufacturer="Justin Aquadro" Manufacturer="Justin Aquadro"
UpgradeCode="0bfb1026-21f2-4552-ad71-ca90aae10a25"> UpgradeCode="0bfb1026-21f2-4552-ad71-ca90aae10a25">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" /> <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

View file

@ -5,8 +5,9 @@ using System.Windows.Forms;
using NBTExplorer.Model; using NBTExplorer.Model;
using NBTExplorer.Vendor.MultiSelectTreeView; using NBTExplorer.Vendor.MultiSelectTreeView;
using NBTExplorer.Windows; using NBTExplorer.Windows;
using Substrate.Nbt; using Substrate.Nbt;
using System.Collections; using System.Collections;
using NBTExplorer.Utility;
namespace NBTExplorer.Controllers namespace NBTExplorer.Controllers
{ {
@ -19,76 +20,77 @@ namespace NBTExplorer.Controllers
{ {
Message = message; Message = message;
} }
} }
public class NodeTreeComparer : IComparer public class NodeTreeComparer : IComparer
{ {
public int OrderForTag(TagType tagID) private NaturalComparer _comparer = new NaturalComparer();
{
switch (tagID) public int OrderForTag(TagType tagID)
{ {
case TagType.TAG_COMPOUND: switch (tagID)
return 0; {
case TagType.TAG_LIST: case TagType.TAG_COMPOUND:
return 1; return 0;
case TagType.TAG_BYTE: case TagType.TAG_LIST:
case TagType.TAG_SHORT: return 1;
case TagType.TAG_INT: case TagType.TAG_BYTE:
case TagType.TAG_LONG: case TagType.TAG_SHORT:
case TagType.TAG_FLOAT: case TagType.TAG_INT:
case TagType.TAG_DOUBLE: case TagType.TAG_LONG:
case TagType.TAG_STRING: case TagType.TAG_FLOAT:
return 2; case TagType.TAG_DOUBLE:
default: case TagType.TAG_STRING:
return 3; return 2;
} default:
} return 3;
}
public int OrderForNode(object node) }
{
if (node is DirectoryDataNode) public int OrderForNode(object node)
{ {
return 0; if (node is DirectoryDataNode)
} {
else return 0;
{ }
return 1; else
} {
} return 1;
}
public int Compare(object x, object y) }
{
TreeNode tx = x as TreeNode; public int Compare(object x, object y)
TreeNode ty = y as TreeNode; {
TagDataNode dx = tx.Tag as TagDataNode; TreeNode tx = x as TreeNode;
TagDataNode dy = ty.Tag as TagDataNode; TreeNode ty = y as TreeNode;
TagDataNode dx = tx.Tag as TagDataNode;
if (dx == null || dy == null) TagDataNode dy = ty.Tag as TagDataNode;
{
int nodeOrder = this.OrderForNode(tx.Tag).CompareTo(this.OrderForNode(ty.Tag)); if (dx == null || dy == null)
if (nodeOrder != 0) {
{ int nodeOrder = this.OrderForNode(tx.Tag).CompareTo(this.OrderForNode(ty.Tag));
return nodeOrder; if (nodeOrder != 0)
} {
else return nodeOrder;
{ }
return tx.Text.CompareTo(ty.Text); 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)); TagType idx = dx.Tag.GetTagType();
if (tagOrder != 0) TagType idy = dy.Tag.GetTagType();
{ int tagOrder = this.OrderForTag(idx).CompareTo(this.OrderForTag(idy));
return tagOrder; if (tagOrder != 0)
} {
else return tagOrder;
{ }
return dx.NodeDisplay.CompareTo(dy.NodeDisplay); else
} {
return _comparer.Compare(dx.NodeDisplay, dy.NodeDisplay);
} }
}
} }
public class NodeTreeController public class NodeTreeController
@ -103,7 +105,7 @@ namespace NBTExplorer.Controllers
public NodeTreeController (TreeView nodeTree) public NodeTreeController (TreeView nodeTree)
{ {
_nodeTree = nodeTree; _nodeTree = nodeTree;
nodeTree.TreeViewNodeSorter = new NodeTreeComparer(); nodeTree.TreeViewNodeSorter = new NodeTreeComparer();
_multiTree = nodeTree as MultiSelectTreeView; _multiTree = nodeTree as MultiSelectTreeView;

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.5.0")] [assembly: AssemblyVersion("2.7.6.0")]
[assembly: AssemblyFileVersion("2.7.5.0")] [assembly: AssemblyFileVersion("2.7.6.0")]

View file

@ -72,6 +72,7 @@
<Compile Include="Data\Nodes\TagLongDataNode.cs" /> <Compile Include="Data\Nodes\TagLongDataNode.cs" />
<Compile Include="Data\Nodes\TagShortDataNode.cs" /> <Compile Include="Data\Nodes\TagShortDataNode.cs" />
<Compile Include="Data\Nodes\TagStringDataNode.cs" /> <Compile Include="Data\Nodes\TagStringDataNode.cs" />
<Compile Include="Utility\NaturalComparer.cs" />
<Compile Include="Utility\SnapshotList.cs" /> <Compile Include="Utility\SnapshotList.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View file

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.3.0")] [assembly: AssemblyVersion("1.0.4.0")]
[assembly: AssemblyFileVersion("1.0.3.0")] [assembly: AssemblyFileVersion("1.0.4.0")]

View file

@ -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<string>, IDisposable
{
private Dictionary<string, string[]> table;
public NaturalComparer ()
{
table = new Dictionary<string, string[]>();
}
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);
}
}
}