[Style] Add "windows-style" sorting to names

This commit is contained in:
Matt Nadareski
2016-09-07 13:35:05 -07:00
parent 50f7bac485
commit 3819819ffa

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace SabreTools.Helper
@@ -374,6 +375,82 @@ namespace SabreTools.Helper
return bytes;
}
/// <summary>
/// https://psycodedeveloper.wordpress.com/2013/04/12/c-numeric-sorting-revisited/
/// </summary>
public static int CompareNumeric(string s, string other)
{
if (s != null && other != null &&
(s = s.Replace(" ", string.Empty)).Length > 0 &&
(other = other.Replace(" ", string.Empty)).Length > 0)
{
int sIndex = 0, otherIndex = 0;
while (sIndex < s.Length)
{
if (otherIndex >= other.Length)
return 1;
if (char.IsDigit(s[sIndex]))
{
if (!char.IsDigit(other[otherIndex]))
return -1;
// Compare the numbers
StringBuilder sBuilder = new StringBuilder(), otherBuilder = new StringBuilder();
while (sIndex < s.Length && char.IsDigit(s[sIndex]))
{
sBuilder.Append(s[sIndex++]);
}
while (otherIndex < other.Length && char.IsDigit(other[otherIndex]))
{
otherBuilder.Append(other[otherIndex++]);
}
long sValue = 0L, otherValue = 0L;
try
{
sValue = Convert.ToInt64(sBuilder.ToString());
}
catch (OverflowException) { sValue = Int64.MaxValue; }
try
{
otherValue = Convert.ToInt64(otherBuilder.ToString());
}
catch (OverflowException) { otherValue = Int64.MaxValue; }
if (sValue < otherValue)
return -1;
else if (sValue > otherValue)
return 1;
}
else if (char.IsDigit(other[otherIndex]))
return 1;
else
{
int difference = string.Compare(s[sIndex].ToString(), other[otherIndex].ToString(), StringComparison.InvariantCultureIgnoreCase);
if (difference > 0)
return 1;
else if (difference < 0)
return -1;
sIndex++;
otherIndex++;
}
}
if (otherIndex < other.Length)
return -1;
}
return 0;
}
#endregion
}
}