[C#] 纯文本查看 复制代码 public class CustomStringComparer : System.Collections.Generic.IComparer<string>
{
#region
/*private List<string> Convert(List<string> lists) {
for (int i = 0; i < lists.Count-1; i++)
{
for (int j = i+1; j < lists.Count; j++)
{
string h1 = lists;
string h2 = lists[j];
if (Compare1(h1,h2))//调用比较算法
{
string temp = lists[j];
[i] lists[j] = lists;
lists = temp;
}
}
}
return lists;
}
private bool Compare1(string fileA, string fileB)
{
if (string.IsNullOrEmpty(fileA) || string.IsNullOrEmpty(fileB))
throw new ArgumentException("Parameters can't be null");
//string fileA = x as string;
//string fileB = y as string;
int resulta = 0;
int resultb = 0;
bool result1 = int.TryParse(fileA, out resulta);
bool result2 = int.TryParse(fileB, out resultb);
if (result1 || result2)
{
if (result1 && result2)
{
return resulta > resultb;
}
else if (!result1)
{
return true;
}
else if (!result2)
{
return false;
}
return false;
}
else
{
char[] arr1 = fileA.ToCharArray();
char[] arr2 = fileB.ToCharArray();
int i = 0, j = 0;
while (i < arr1.Length && j < arr2.Length)
{
if (char.IsDigit(arr1) && char.IsDigit(arr2[j]))
{
string s1 = "", s2 = "";
while (i < arr1.Length && char.IsDigit(arr1))
{
s1 += arr1;
i++;
}
while (j < arr2.Length && char.IsDigit(arr2[j]))
{
s2 += arr2[j];
j++;
}
if (int.Parse(s1) > int.Parse(s2))
{
return true;
}
else if(int.Parse(s1) < int.Parse(s2))
{
return false;
}
}
else
{
if (!char.IsDigit(arr1) && !char.IsDigit(arr2[j]))
{
if (arr1 > arr2[j])
{
return true;
}
else if(arr1 < arr2[j])
{
return false;
}
}
else if (char.IsDigit(arr1))
{
return false;
}
else if(char.IsDigit(arr2[j]))
{
return true;
}
i++;
j++;
}
}
if (arr1.Length > arr2.Length)
{
return true;
}
else
{
return false;
}
}
}*/
#endregion
public int Compare(string fileA, string fileB)
{
if (string.IsNullOrEmpty(fileA) || string.IsNullOrEmpty(fileB))
throw new ArgumentException("Parameters can't be null");
int resulta = 0;
int resultb = 0;
bool result1 = int.TryParse(fileA, out resulta);
bool result2 = int.TryParse(fileB, out resultb);
if (result1 || result2)//任意一个字符串为数值
{
//判断是否有为非数值
if (!result2)
{
return -1;
}
else if (!result1)
{
return 1;
}
else//都是数值
{
return resulta > resultb ? 1 : resulta < resultb ? -1 : 0;
}
}
else//都包含非数值类型字符
{
char[] arr1 = fileA.ToCharArray();
char[] arr2 = fileB.ToCharArray();
int i = 0, j = 0;
while (i < arr1.Length && j < arr2.Length)
{
if (char.IsDigit(arr1) && char.IsDigit(arr2[j]))
{
string s1 = "", s2 = "";
while (i < arr1.Length && char.IsDigit(arr1))
{
s1 += arr1;
i++;
}
while (j < arr2.Length && char.IsDigit(arr2[j]))
{
s2 += arr2[j];
j++;
}
if (int.Parse(s1) > int.Parse(s2))
{
return 1;
}
else if (int.Parse(s1) < int.Parse(s2))
{
return -1;
}
}
else
{
if (!char.IsDigit(arr1) && !char.IsDigit(arr2[j]))
{
if (arr1 > arr2[j])
{
return 1;
}
else if (arr1 < arr2[j])
{
return -1;
}
}
else if (char.IsDigit(arr1))
{
return -1;
}
else if (char.IsDigit(arr2[j]))
{
return 1;
}
i++;
j++;
}
}
return arr1.Length > arr2.Length ? 1 : arr1.Length < arr2.Length ? -1 : 0;
}
}
}
使用方法:
List<T>.OrderByDescending(T=>T.排序字段,new CustomStringComparer()
|