下面一段C#代码是模拟Window排序的算法,不包括外围的排序,只是两个String的比较。
写的比较粗糙,请大家提出宝贵意见。
window中的排序,优先级是这样的,
特殊符号 〉 数字 〉 字母 〉 字符
其中对数字的处理比较特别举例如下:
10a,1a进行比较,1a 的优先级要大于10a,这是因为windows排序把1a拆分为 1和a, 10a拆分为 10和a然后分别比较。如果用字典排序,10a的优先级要比1a的优先级高,
-------------------------------------------------------------------------------------------------------------------
private int StringCompare(string A, string B, bool sortType)
{
string strA = string.Empty;
string strB = string.Empty;
int checkResult;
int result;
bool flagA;
bool flagB;
Dictionary<int, string> libA = new Dictionary<int, string>();
Dictionary<int, string> libB = new Dictionary<int, string>();
checkResult = CheckStringValue(A, B, sortType);
if (2 != checkResult)
{
return checkResult;
}
CutString(A, 0, libA);
CutString(B, 0, libB);
// loop for compare string
for (int i = 0; (i < libA.Count) && (i < libB.Count); ++i)
{
libA.TryGetValue(i, out strA);
libB.TryGetValue(i, out strB);
flagA = Char.IsLetterOrDigit(strA, 0);
flagB = Char.IsLetterOrDigit(strB, 0);
if (!flagA || !flagB)
{
if (!flagA && !flagB)
{
result = string.Compare(strA, strB);
}
else
{
if (!flagA)
{
return -1;
}
else
{
return 1;
}
}
}
else
{
flagA = Char.IsDigit(strA, 0);
flagB = Char.IsDigit(strB, 0);
//Both substrings is digit
if (flagA || flagB)
{
if (flagA && flagB)
{
result = DigitalStringCompare(strA, strB);
}
else
{
// Only strA is Digit
if (flagA)
{
result = -1;
}
else // Only strB is Digit
{
result = 1;
}
}
}
else
{
result = string.Compare(strA, strB);
}
if (0 != result)
{
return result;
}
}
} //end for
if (libA.Count == libB.Count)
{
return 0;
}
if (libA.Count > libB.Count)
{
return 1;
}
return -1;
}
private void CutString(string str, int beginIndex, Dictionary<int, string> lib)
{
string strTemp = String.Empty;
int counter = beginIndex;
if (!Char.IsLetterOrDigit(str, beginIndex))
{
while ((counter < (str.Length)) && !Char.IsLetterOrDigit(str, counter))
{
strTemp += str.Substring(counter, 1);
++counter;
}
}
else
{
if (Char.IsDigit(str, beginIndex))
{
while ((counter < (str.Length)) && Char.IsDigit(str, counter))
{
strTemp += str.Substring(counter, 1);
++counter;
}
}
else
{
while ((counter < (str.Length)) && !Char.IsDigit(str, counter))
{
strTemp += str.Substring(counter, 1);
++counter;
}
}
}
if (strTemp != string.Empty)
{
lib.Add(lib.Count, strTemp);
}
if (counter < (str.Length))
{
CutString(str, counter, lib);
}
return;
}
private int DigitalStringCompare(string A, string B)
{
int a;
int b;
a = Convert.ToInt32(A);
b = Convert.ToInt32(B);
if (a > b)
{
return 1;
}
if (a < b)
{
return -1;
}
if (A.Length < B.Length)
{
return 1;
}
if (A.Length > B.Length)
{
return -1;
}
return 0;
}
private short CheckStringValue(string A, string B, bool sortType)
{
if ((null == A) || (null == B))
{
if ((null == A) && (null == B))
{
return 0;
}
if (null == A)
{
if (sortType)
{
return 1;
}
else
{
return -1;
}
}
if (null == B)
{
if (sortType)
{
return -1;
}
else
{
return 1;
}
}
}
else
{
if ((string.Empty == A) && (string.Empty == B))
{
return 0;
}
if (string.Empty == A)
{
if (sortType)
{
return 1;
}
else
{
return -1;
}
}
if (string.Empty == B)
{
if (sortType)
{
return -1;
}
else
{
return 1;
}
}
}
return 2;
}