diff --git a/SabreTools.Helper/Dats/DatFile.cs b/SabreTools.Helper/Dats/DatFile.cs index 0840cc52..57eb8c83 100644 --- a/SabreTools.Helper/Dats/DatFile.cs +++ b/SabreTools.Helper/Dats/DatFile.cs @@ -5757,7 +5757,7 @@ namespace SabreTools.Helper.Dats // Get a properly sorted set of keys List keys = Files.Keys.ToList(); - keys.Sort(Style.CompareNumeric); + keys.Sort(new NaturalComparer()); foreach (string key in keys) { diff --git a/SabreTools.Helper/Dats/DatItem.cs b/SabreTools.Helper/Dats/DatItem.cs index 59935407..46ee0d1d 100644 --- a/SabreTools.Helper/Dats/DatItem.cs +++ b/SabreTools.Helper/Dats/DatItem.cs @@ -538,6 +538,7 @@ namespace SabreTools.Helper.Dats { try { + NaturalComparer nc = new NaturalComparer(); if (x.SystemID == y.SystemID) { 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))) { - 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)) { @@ -564,16 +565,16 @@ namespace SabreTools.Helper.Dats { 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) { diff --git a/SabreTools.Helper/External/NaturalSort/NaturalComparer.cs b/SabreTools.Helper/External/NaturalSort/NaturalComparer.cs index 36eb5ecf..c735f393 100644 --- a/SabreTools.Helper/External/NaturalSort/NaturalComparer.cs +++ b/SabreTools.Helper/External/NaturalSort/NaturalComparer.cs @@ -11,8 +11,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; +using SabreTools.Helper.Tools; + namespace NaturalSort { public class NaturalComparer : Comparer, IDisposable @@ -40,13 +43,13 @@ namespace NaturalSort if (!table.TryGetValue(x, out x1)) { //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); } if (!table.TryGetValue(y, out y1)) { //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); } @@ -76,12 +79,12 @@ namespace NaturalSort int x, y; if (!int.TryParse(left, out x)) { - return left.CompareTo(right); + return Style.CompareNumeric(left, right); } if (!int.TryParse(right, out y)) { - return left.CompareTo(right); + return Style.CompareNumeric(left, right); } return x.CompareTo(y); diff --git a/SabreTools.Helper/External/NaturalSort/NaturalReversedComparer.cs b/SabreTools.Helper/External/NaturalSort/NaturalReversedComparer.cs index e018a863..705acf93 100644 --- a/SabreTools.Helper/External/NaturalSort/NaturalReversedComparer.cs +++ b/SabreTools.Helper/External/NaturalSort/NaturalReversedComparer.cs @@ -11,8 +11,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; +using SabreTools.Helper.Tools; + namespace NaturalSort { public class NaturalReversedComparer : Comparer, IDisposable @@ -40,13 +43,13 @@ namespace NaturalSort if (!table.TryGetValue(x, out x1)) { //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); } if (!table.TryGetValue(y, out y1)) { //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); } @@ -76,12 +79,12 @@ namespace NaturalSort int x, y; if (!int.TryParse(left, out x)) { - return right.CompareTo(left); + return Style.CompareNumeric(right, left); } if (!int.TryParse(right, out y)) { - return right.CompareTo(left); + return Style.CompareNumeric(right, left); } return -x.CompareTo(y); diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index 78ce29e0..dd3311d6 100644 --- a/SabreTools.Helper/Tools/FileTools.cs +++ b/SabreTools.Helper/Tools/FileTools.cs @@ -44,12 +44,12 @@ namespace SabreTools.Helper.Tools { // Take care of the files in the top directory List toadd = Directory.EnumerateFiles(directory, "*", SearchOption.TopDirectoryOnly).ToList(); - toadd.Sort(Style.CompareNumeric); + toadd.Sort(new NaturalComparer()); infiles.AddRange(toadd); // Then recurse through and add from the directories List dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList(); - dirs.Sort(Style.CompareNumeric); + dirs.Sort(new NaturalComparer()); foreach (string dir in dirs) { infiles = RetrieveFiles(dir, infiles); diff --git a/SabreTools.Helper/Tools/Style.cs b/SabreTools.Helper/Tools/Style.cs index 5df9aaf3..fdb019de 100644 --- a/SabreTools.Helper/Tools/Style.cs +++ b/SabreTools.Helper/Tools/Style.cs @@ -278,9 +278,10 @@ namespace SabreTools.Helper.Tools /// /// Compare strings as numeric /// - /// - /// - /// + /// First string to compare + /// Second string to compare + /// -1 if s1 comes before s2, 0 if s1 and s2 are equal, 1 if s1 comes after s2 + /// I want to be able to handle paths properly with no issue, can I do a recursive call based on separated by path separator? public static int CompareNumeric(string s1, string s2) { // We want to normalize the strings, so we set both to lower case