diff --git a/SabreTools.Helper/Dats/DatFile.cs b/SabreTools.Helper/Dats/DatFile.cs index 57eb8c83..0840cc52 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(new NaturalComparer()); + keys.Sort(Style.CompareNumeric); foreach (string key in keys) { diff --git a/SabreTools.Helper/Dats/DatItem.cs b/SabreTools.Helper/Dats/DatItem.cs index 46ee0d1d..59935407 100644 --- a/SabreTools.Helper/Dats/DatItem.cs +++ b/SabreTools.Helper/Dats/DatItem.cs @@ -538,7 +538,6 @@ namespace SabreTools.Helper.Dats { try { - NaturalComparer nc = new NaturalComparer(); if (x.SystemID == y.SystemID) { if (x.SourceID == y.SourceID) @@ -549,9 +548,9 @@ namespace SabreTools.Helper.Dats { if (Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)) == Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))) { - return nc.Compare(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name))); + return Style.CompareNumeric(Path.GetFileName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetFileName(Style.RemovePathUnsafeCharacters(y.Name))); } - return nc.Compare(Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(x.Name)), Path.GetDirectoryName(Style.RemovePathUnsafeCharacters(y.Name))); + return Style.CompareNumeric(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)) { @@ -565,16 +564,16 @@ namespace SabreTools.Helper.Dats { if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name)) { - return nc.Compare(Path.GetFileName(x.Name), Path.GetFileName(y.Name)); + return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name)); } - return nc.Compare(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name)); + return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name)); } } - return nc.Compare(x.Machine.Name, y.Machine.Name); + return Style.CompareNumeric(x.Machine.Name, y.Machine.Name); } - 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.SourceID - y.SourceID); } - return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID); + return (norename ? Style.CompareNumeric(x.Machine.Name, y.Machine.Name) : x.SystemID - y.SystemID); } catch (Exception) { diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index dd3311d6..78ce29e0 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(new NaturalComparer()); + toadd.Sort(Style.CompareNumeric); infiles.AddRange(toadd); // Then recurse through and add from the directories List dirs = Directory.EnumerateDirectories(directory, "*", SearchOption.TopDirectoryOnly).ToList(); - dirs.Sort(new NaturalComparer()); + dirs.Sort(Style.CompareNumeric); foreach (string dir in dirs) { infiles = RetrieveFiles(dir, infiles); diff --git a/SabreTools.Helper/Tools/Style.cs b/SabreTools.Helper/Tools/Style.cs index b492e6ba..0978f0b2 100644 --- a/SabreTools.Helper/Tools/Style.cs +++ b/SabreTools.Helper/Tools/Style.cs @@ -275,6 +275,66 @@ namespace SabreTools.Helper.Tools return new string(s.Where(c => !invalidPath.Contains(c)).ToArray()); } + /// + /// Compare strings as numeric + /// + /// + /// + /// + public static int CompareNumeric(string s1, string s2) + { + // If the strings are the same exactly, return + if (s1 == s2) + { + return 0; + } + + // If one is null, then say that's less than + if (s1 == null) + { + return -1; + } + if (s2 == null) + { + return 1; + } + + // Otherwise, loop through until we have an answer + for (int i = 0; i < s1.Length && i < s2.Length; i++) + { + int s1c = s1[i]; + int s2c = s2[i]; + + // If the characters are the same, continue + if (s1c == s2c) + { + continue; + } + + // If they're different, check which one was larger + if (s1c > s2c) + { + return 1; + } + if (s1c < s2c) + { + return -1; + } + } + + // If we got out here, then it looped through at least one of the strings + if (s1.Length > s2.Length) + { + return 1; + } + if (s1.Length < s2.Length) + { + return -1; + } + + return 0; + } + /// /// Convert all characters that are not considered XML-safe ///