Added a sorting mechanism to emulate how windows sorts files. Refactored cache to support chapter folders as well.
This commit is contained in:
parent
6020697d7d
commit
f737f662df
11 changed files with 237 additions and 32 deletions
17
API/Comparators/NumericComparer.cs
Normal file
17
API/Comparators/NumericComparer.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using System.Collections;
|
||||
|
||||
namespace API.Comparators
|
||||
{
|
||||
public class NumericComparer : IComparer
|
||||
{
|
||||
|
||||
public int Compare(object x, object y)
|
||||
{
|
||||
if((x is string xs) && (y is string ys))
|
||||
{
|
||||
return StringLogicalComparer.Compare(xs, ys);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
130
API/Comparators/StringLogicalComparer.cs
Normal file
130
API/Comparators/StringLogicalComparer.cs
Normal file
|
@ -0,0 +1,130 @@
|
|||
//(c) Vasian Cepa 2005
|
||||
// Version 2
|
||||
// Taken from: https://www.codeproject.com/Articles/11016/Numeric-String-Sort-in-C
|
||||
|
||||
using System;
|
||||
|
||||
namespace API.Comparators
|
||||
{
|
||||
public static class StringLogicalComparer
|
||||
{
|
||||
public static int Compare(string s1, string s2)
|
||||
{
|
||||
//get rid of special cases
|
||||
if((s1 == null) && (s2 == null)) return 0;
|
||||
if(s1 == null) return -1;
|
||||
if(s2 == null) return 1;
|
||||
|
||||
if (string.IsNullOrEmpty(s1) && string.IsNullOrEmpty(s2)) return 0;
|
||||
if (string.IsNullOrEmpty(s1)) return -1;
|
||||
if (string.IsNullOrEmpty(s2)) return -1;
|
||||
|
||||
//WE style, special case
|
||||
bool sp1 = Char.IsLetterOrDigit(s1, 0);
|
||||
bool sp2 = Char.IsLetterOrDigit(s2, 0);
|
||||
if(sp1 && !sp2) return 1;
|
||||
if(!sp1 && sp2) return -1;
|
||||
|
||||
int i1 = 0, i2 = 0; //current index
|
||||
while(true)
|
||||
{
|
||||
bool c1 = Char.IsDigit(s1, i1);
|
||||
bool c2 = Char.IsDigit(s2, i2);
|
||||
var r = 0; // temp result
|
||||
if(!c1 && !c2)
|
||||
{
|
||||
bool letter1 = Char.IsLetter(s1, i1);
|
||||
bool letter2 = Char.IsLetter(s2, i2);
|
||||
if((letter1 && letter2) || (!letter1 && !letter2))
|
||||
{
|
||||
if(letter1 && letter2)
|
||||
{
|
||||
r = Char.ToLower(s1[i1]).CompareTo(Char.ToLower(s2[i2]));
|
||||
}
|
||||
else
|
||||
{
|
||||
r = s1[i1].CompareTo(s2[i2]);
|
||||
}
|
||||
if(r != 0) return r;
|
||||
}
|
||||
else if(!letter1 && letter2) return -1;
|
||||
else if(letter1 && !letter2) return 1;
|
||||
}
|
||||
else if(c1 && c2)
|
||||
{
|
||||
r = CompareNum(s1, ref i1, s2, ref i2);
|
||||
if(r != 0) return r;
|
||||
}
|
||||
else if(c1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if(c2)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
i1++;
|
||||
i2++;
|
||||
if((i1 >= s1.Length) && (i2 >= s2.Length))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(i1 >= s1.Length)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if(i2 >= s2.Length)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int CompareNum(string s1, ref int i1, string s2, ref int i2)
|
||||
{
|
||||
int nzStart1 = i1, nzStart2 = i2; // nz = non zero
|
||||
int end1 = i1, end2 = i2;
|
||||
|
||||
ScanNumEnd(s1, i1, ref end1, ref nzStart1);
|
||||
ScanNumEnd(s2, i2, ref end2, ref nzStart2);
|
||||
var start1 = i1; i1 = end1 - 1;
|
||||
var start2 = i2; i2 = end2 - 1;
|
||||
|
||||
var nzLength1 = end1 - nzStart1;
|
||||
var nzLength2 = end2 - nzStart2;
|
||||
|
||||
if(nzLength1 < nzLength2) return -1;
|
||||
if(nzLength1 > nzLength2) return 1;
|
||||
|
||||
for(int j1 = nzStart1,j2 = nzStart2; j1 <= i1; j1++,j2++)
|
||||
{
|
||||
var r = s1[j1].CompareTo(s2[j2]);
|
||||
if(r != 0) return r;
|
||||
}
|
||||
// the nz parts are equal
|
||||
var length1 = end1 - start1;
|
||||
var length2 = end2 - start2;
|
||||
if(length1 == length2) return 0;
|
||||
if(length1 > length2) return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
//lookahead
|
||||
private static void ScanNumEnd(string s, int start, ref int end, ref int nzStart)
|
||||
{
|
||||
nzStart = start;
|
||||
end = start;
|
||||
bool countZeros = true;
|
||||
while(Char.IsDigit(s, end))
|
||||
{
|
||||
if(countZeros && s[end].Equals('0'))
|
||||
{
|
||||
nzStart++;
|
||||
}
|
||||
else countZeros = false;
|
||||
end++;
|
||||
if(end >= s.Length) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue