Files
SabreTools/SabreTools.Library/External/NaturalSort/NaturalReversedComparer.cs

98 lines
2.1 KiB
C#
Raw Normal View History

/*
*
* 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;
using System.Linq;
using System.Text.RegularExpressions;
2017-05-04 02:41:11 -07:00
using SabreTools.Library.Tools;
namespace NaturalSort
{
public class NaturalReversedComparer : Comparer<string>, IDisposable
{
private Dictionary<string, string[]> table;
public NaturalReversedComparer()
{
table = new Dictionary<string, string[]>();
}
public void Dispose()
{
table.Clear();
table = null;
}
public override int Compare(string x, string y)
{
2017-06-02 13:11:59 -07:00
if (y.ToLowerInvariant() == x.ToLowerInvariant())
{
2017-06-02 13:11:59 -07:00
return y.CompareTo(x);
}
2017-06-16 17:05:48 -07:00
if (!table.TryGetValue(x, out string[] x1))
{
//x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
x1 = Regex.Split(x.ToLowerInvariant(), "([0-9]+)").Where(s => !String.IsNullOrWhiteSpace(s)).ToArray();
table.Add(x, x1);
}
2017-06-16 17:05:48 -07:00
if (!table.TryGetValue(y, out string[] y1))
{
//y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
y1 = Regex.Split(y.ToLowerInvariant(), "([0-9]+)").Where(s => !String.IsNullOrWhiteSpace(s)).ToArray();
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 y.CompareTo(x);
}
}
private static int PartCompare(string left, string right)
{
2017-06-16 17:05:48 -07:00
if (!long.TryParse(left, out long x))
{
2017-11-08 00:27:00 -08:00
return Utilities.CompareNumeric(right, left);
}
2017-06-16 17:05:48 -07:00
if (!long.TryParse(right, out long y))
{
2017-11-08 00:27:00 -08:00
return Utilities.CompareNumeric(right, left);
}
// If we have an equal part, then make sure that "longer" ones are taken into account
if (y.CompareTo(x) == 0)
{
return right.Length - left.Length;
}
return y.CompareTo(x);
}
}
}