[NaturalComparer] Use CompareNumeric instead of Compare, where possible

This commit is contained in:
Matt Nadareski
2016-10-28 12:59:52 -07:00
parent 1868aa17c5
commit 12ba858825
6 changed files with 29 additions and 21 deletions

View File

@@ -5757,7 +5757,7 @@ namespace SabreTools.Helper.Dats
// Get a properly sorted set of keys // Get a properly sorted set of keys
List<string> keys = Files.Keys.ToList(); List<string> keys = Files.Keys.ToList();
keys.Sort(Style.CompareNumeric); keys.Sort(new NaturalComparer());
foreach (string key in keys) foreach (string key in keys)
{ {

View File

@@ -538,6 +538,7 @@ namespace SabreTools.Helper.Dats
{ {
try try
{ {
NaturalComparer nc = new NaturalComparer();
if (x.SystemID == y.SystemID) if (x.SystemID == y.SystemID)
{ {
if (x.SourceID == y.SourceID) if (x.SourceID == y.SourceID)
@@ -548,9 +549,9 @@ namespace SabreTools.Helper.Dats
{ {
if (Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))) if (Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name)))
{ {
return Style.CompareNumeric(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name))); return nc.Compare(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name)));
} }
return Style.CompareNumeric(Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))); return nc.Compare(Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name)));
} }
else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk)) else if ((x.Type == ItemType.Rom || x.Type == ItemType.Disk) && (y.Type != ItemType.Rom && y.Type != ItemType.Disk))
{ {
@@ -564,16 +565,16 @@ namespace SabreTools.Helper.Dats
{ {
if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name)) if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name))
{ {
return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name)); return nc.Compare(Path.GetFileName(x.Name), Path.GetFileName(y.Name));
} }
return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name)); return nc.Compare(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name));
} }
} }
return Style.CompareNumeric(x.Machine.Name, y.Machine.Name); return nc.Compare(x.Machine.Name, y.Machine.Name);
} }
return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.SourceID - y.SourceID); return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SourceID - y.SourceID);
} }
return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID); return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID);
} }
catch (Exception) catch (Exception)
{ {

View File

@@ -11,8 +11,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using SabreTools.Helper.Tools;
namespace NaturalSort namespace NaturalSort
{ {
public class NaturalComparer : Comparer<string>, IDisposable public class NaturalComparer : Comparer<string>, IDisposable
@@ -40,13 +43,13 @@ namespace NaturalSort
if (!table.TryGetValue(x, out x1)) if (!table.TryGetValue(x, out x1))
{ {
//x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); //x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
x1 = Regex.Split(x, "([0-9]+)"); x1 = Regex.Split(x, "([0-9]+)").Where(s => s != "").ToArray();
table.Add(x, x1); table.Add(x, x1);
} }
if (!table.TryGetValue(y, out y1)) if (!table.TryGetValue(y, out y1))
{ {
//y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); //y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
y1 = Regex.Split(y, "([0-9]+)"); y1 = Regex.Split(y, "([0-9]+)").Where(s => s != "").ToArray();
table.Add(y, y1); table.Add(y, y1);
} }
@@ -76,12 +79,12 @@ namespace NaturalSort
int x, y; int x, y;
if (!int.TryParse(left, out x)) if (!int.TryParse(left, out x))
{ {
return left.CompareTo(right); return Style.CompareNumeric(left, right);
} }
if (!int.TryParse(right, out y)) if (!int.TryParse(right, out y))
{ {
return left.CompareTo(right); return Style.CompareNumeric(left, right);
} }
return x.CompareTo(y); return x.CompareTo(y);

View File

@@ -11,8 +11,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using SabreTools.Helper.Tools;
namespace NaturalSort namespace NaturalSort
{ {
public class NaturalReversedComparer : Comparer<string>, IDisposable public class NaturalReversedComparer : Comparer<string>, IDisposable
@@ -40,13 +43,13 @@ namespace NaturalSort
if (!table.TryGetValue(x, out x1)) if (!table.TryGetValue(x, out x1))
{ {
//x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); //x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
x1 = Regex.Split(x, "([0-9]+)"); x1 = Regex.Split(x, "([0-9]+)").Where(s => s != "").ToArray();
table.Add(x, x1); table.Add(x, x1);
} }
if (!table.TryGetValue(y, out y1)) if (!table.TryGetValue(y, out y1))
{ {
//y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); //y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
y1 = Regex.Split(y, "([0-9]+)"); y1 = Regex.Split(y, "([0-9]+)").Where(s => s != "").ToArray();
table.Add(y, y1); table.Add(y, y1);
} }
@@ -76,12 +79,12 @@ namespace NaturalSort
int x, y; int x, y;
if (!int.TryParse(left, out x)) if (!int.TryParse(left, out x))
{ {
return right.CompareTo(left); return Style.CompareNumeric(right, left);
} }
if (!int.TryParse(right, out y)) if (!int.TryParse(right, out y))
{ {
return right.CompareTo(left); return Style.CompareNumeric(right, left);
} }
return -x.CompareTo(y); return -x.CompareTo(y);

View File

@@ -44,12 +44,12 @@ namespace SabreTools.Helper.Tools
{ {
// Take care of the files in the top directory // Take care of the files in the top directory
List<string> toadd = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly).ToList(); List<string> toadd = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly).ToList();
toadd.Sort(Style.CompareNumeric); toadd.Sort(new NaturalComparer());
infiles.AddRange(toadd); infiles.AddRange(toadd);
// Then recurse through and add from the directories // Then recurse through and add from the directories
List<string> dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList(); List<string> dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList();
dirs.Sort(Style.CompareNumeric); dirs.Sort(new NaturalComparer());
foreach (string dir in dirs) foreach (string dir in dirs)
{ {
infiles = RetrieveFiles(dir, infiles); infiles = RetrieveFiles(dir, infiles);

View File

@@ -278,9 +278,10 @@ namespace SabreTools.Helper.Tools
/// <summary> /// <summary>
/// Compare strings as numeric /// Compare strings as numeric
/// </summary> /// </summary>
/// <param name="s1"></param> /// <param name="s1">First string to compare</param>
/// <param name="s2"></param> /// <param name="s2">Second string to compare</param>
/// <returns></returns> /// <returns>-1 if s1 comes before s2, 0 if s1 and s2 are equal, 1 if s1 comes after s2</returns>
/// <remarks>I want to be able to handle paths properly with no issue, can I do a recursive call based on separated by path separator?</remarks>
public static int CompareNumeric(string s1, string s2) public static int CompareNumeric(string s1, string s2)
{ {
// We want to normalize the strings, so we set both to lower case // We want to normalize the strings, so we set both to lower case