Update alphanum sort algoritm

This commit is contained in:
Laura Hausmann 2021-07-25 23:10:31 +02:00
parent e8fbfc3bc9
commit ed04d034da
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -24,7 +24,7 @@ namespace webmusic.Pages {
Response.Redirect("/Error");
return;
}
if (Path.EndsWith(".m3u"))
Path = Path.Substring(0, Path.Length - 4);
@ -41,84 +41,69 @@ namespace webmusic.Pages {
Files.RemoveAll(p => p.EndsWith(".m3u"));
Files.RemoveAll(p => p.EndsWith(".lrc"));
Files.RemoveAll(p => p.StartsWith("."));
Files.Sort(new AlphanumComparatorFast());
Files.Sort(new NaturalSortComparer());
}
public static string Encode(string str) => str.Replace("\"", "%22")
.Replace("'", "%27")
.Replace("?", "%3F")
.Replace("&", "%26")
.Replace(" ", "%20");
public static string Encode(string str) => str.Replace("\"", "%22").Replace("'", "%27").Replace("?", "%3F").Replace("&", "%26").Replace(" ", "%20");
private class AlphanumComparatorFast : IComparer<string> {
public int Compare(string x, string y) {
var s1 = x;
if (s1 == null)
private class NaturalSortComparer : IComparer<string>, IDisposable {
private readonly bool _isAscending;
public NaturalSortComparer(bool inAscendingOrder = true) {
this._isAscending = inAscendingOrder;
}
int IComparer<string>.Compare(string x, string y) {
if (x == y)
return 0;
if (!(y is { } s2))
return 0;
string[] x1, y1;
var len1 = s1.Length;
var len2 = s2.Length;
var marker1 = 0;
var marker2 = 0;
// Walk through two the strings with two markers.
while (marker1 < len1 && marker2 < len2) {
var ch1 = s1[marker1];
var ch2 = s2[marker2];
// Some buffers we can build up characters in for each chunk.
var space1 = new char[len1];
var loc1 = 0;
var space2 = new char[len2];
var loc2 = 0;
// Walk through all following characters that are digits or
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do {
space1[loc1++] = ch1;
marker1++;
if (marker1 < len1)
ch1 = s1[marker1];
else
break;
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
do {
space2[loc2++] = ch2;
marker2++;
if (marker2 < len2)
ch2 = s2[marker2];
else
break;
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
// If we have collected numbers, compare them numerically.
// Otherwise, if we have strings, compare them alphabetically.
var str1 = new string(space1);
var str2 = new string(space2);
int result;
if (char.IsDigit(space1[0]) && char.IsDigit(space2[0])) {
var thisNumericChunk = int.Parse(str1);
var thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else {
result = string.Compare(str1, str2, StringComparison.Ordinal);
}
if (result != 0)
return result;
if (!_table.TryGetValue(x!, out x1)) {
x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)");
_table.Add(x, x1);
}
return len1 - len2;
if (!_table.TryGetValue(y!, out y1)) {
y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)");
_table.Add(y, y1);
}
int returnVal;
for (var i = 0; i < x1.Length && i < y1.Length; i++) {
if (x1[i] != y1[i]) {
returnVal = PartCompare(x1[i], y1[i]);
return _isAscending ? returnVal : -returnVal;
}
}
if (y1.Length > x1.Length) {
returnVal = 1;
}
else if (x1.Length > y1.Length) {
returnVal = -1;
}
else {
returnVal = 0;
}
return _isAscending ? returnVal : -returnVal;
}
private static int PartCompare(string left, string right) {
int x, y;
if (!int.TryParse(left, out x))
return string.Compare(left, right, StringComparison.Ordinal);
return !int.TryParse(right, out y) ? string.Compare(left, right, StringComparison.Ordinal) : x.CompareTo(y);
}
private Dictionary<string, string[]> _table = new();
public void Dispose() {
_table.Clear();
_table = null;
}
}
}