2020-07-15 09:41:59 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
2024-02-28 19:56:33 -05:00
|
|
|
|
// Ported to SabreTools.Matching. Remove once published.
|
2020-07-15 09:41:59 -07:00
|
|
|
|
namespace NaturalSort
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class NaturalComparerUtil
|
|
|
|
|
|
{
|
2023-08-12 00:55:41 -04:00
|
|
|
|
public static int CompareNumeric(string s1, string s2)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Save the orginal strings, for later comparison
|
|
|
|
|
|
string s1orig = s1;
|
|
|
|
|
|
string s2orig = s2;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// We want to normalize the strings, so we set both to lower case
|
|
|
|
|
|
s1 = s1.ToLowerInvariant();
|
|
|
|
|
|
s2 = s2.ToLowerInvariant();
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// If the strings are the same exactly, return
|
|
|
|
|
|
if (s1 == s2)
|
|
|
|
|
|
return s1orig.CompareTo(s2orig);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// If one is null, then say that's less than
|
|
|
|
|
|
if (s1 == null)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
if (s2 == null)
|
|
|
|
|
|
return 1;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// Now split into path parts after converting AltDirSeparator to DirSeparator
|
|
|
|
|
|
s1 = s1.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
|
|
|
|
|
s2 = s2.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
|
|
|
|
|
|
string[] s1parts = s1.Split(Path.DirectorySeparatorChar);
|
|
|
|
|
|
string[] s2parts = s2.Split(Path.DirectorySeparatorChar);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// Then compare each part in turn
|
|
|
|
|
|
for (int j = 0; j < s1parts.Length && j < s2parts.Length; j++)
|
|
|
|
|
|
{
|
|
|
|
|
|
int compared = CompareNumericPart(s1parts[j], s2parts[j]);
|
|
|
|
|
|
if (compared != 0)
|
|
|
|
|
|
return compared;
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// If we got out here, then it looped through at least one of the strings
|
|
|
|
|
|
if (s1parts.Length > s2parts.Length)
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
if (s1parts.Length < s2parts.Length)
|
|
|
|
|
|
return -1;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
return s1orig.CompareTo(s2orig);
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
private static int CompareNumericPart(string s1, string s2)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 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];
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// If the characters are the same, continue
|
|
|
|
|
|
if (s1c == s2c)
|
|
|
|
|
|
continue;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// If they're different, check which one was larger
|
|
|
|
|
|
if (s1c > s2c)
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
if (s1c < s2c)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
// 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;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
2023-08-12 00:55:41 -04:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|