2016-09-26 12:11:33 -07:00
|
|
|
|
/*
|
|
|
|
|
|
*
|
|
|
|
|
|
* Links for info and original source code:
|
|
|
|
|
|
*
|
|
|
|
|
|
* https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
|
|
|
|
|
|
* http://www.codeproject.com/Articles/22517/Natural-Sort-Comparer
|
|
|
|
|
|
*
|
|
|
|
|
|
* Exact code implementation used with permission, originally by motoschifo
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2016-10-28 12:59:52 -07:00
|
|
|
|
using System.Linq;
|
2016-09-26 12:11:33 -07:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
2016-10-28 12:59:52 -07:00
|
|
|
|
using SabreTools.Helper.Tools;
|
|
|
|
|
|
|
2016-10-24 12:58:57 -07:00
|
|
|
|
namespace NaturalSort
|
2016-09-26 12:11:33 -07:00
|
|
|
|
{
|
|
|
|
|
|
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))
|
|
|
|
|
|
{
|
2017-01-27 15:42:07 -08:00
|
|
|
|
//x1 = Regex.Split(x.Replace(" ", String.Empty), "([0-9]+)");
|
|
|
|
|
|
x1 = Regex.Split(x, "([0-9]+)").Where(s => s != String.Empty).ToArray();
|
2016-09-26 12:11:33 -07:00
|
|
|
|
table.Add(x, x1);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!table.TryGetValue(y, out y1))
|
|
|
|
|
|
{
|
2017-01-27 15:42:07 -08:00
|
|
|
|
//y1 = Regex.Split(y.Replace(" ", String.Empty), "([0-9]+)");
|
|
|
|
|
|
y1 = Regex.Split(y, "([0-9]+)").Where(s => s != String.Empty).ToArray();
|
2016-09-26 12:11:33 -07:00
|
|
|
|
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))
|
|
|
|
|
|
{
|
2016-10-28 12:59:52 -07:00
|
|
|
|
return Style.CompareNumeric(left, right);
|
2016-09-26 12:11:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!int.TryParse(right, out y))
|
|
|
|
|
|
{
|
2016-10-28 12:59:52 -07:00
|
|
|
|
return Style.CompareNumeric(left, right);
|
2016-09-26 12:11:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return x.CompareTo(y);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|