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="*"
Name="NBTExplorer"
Language="1033"
Version="2.7.5.0"
Version="2.7.6.0"
Manufacturer="Justin Aquadro"
UpgradeCode="0bfb1026-21f2-4552-ad71-ca90aae10a25">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

View file

@ -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;

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
// 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")]

View file

@ -72,6 +72,7 @@
<Compile Include="Data\Nodes\TagLongDataNode.cs" />
<Compile Include="Data\Nodes\TagShortDataNode.cs" />
<Compile Include="Data\Nodes\TagStringDataNode.cs" />
<Compile Include="Utility\NaturalComparer.cs" />
<Compile Include="Utility\SnapshotList.cs" />
</ItemGroup>
<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
// 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")]

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);
}
}
}