mirror of https://github.com/leiurayer/downkyi
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
76 lines
2.2 KiB
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DownKyi.Core.Utils
|
|
{
|
|
public class StringLogicalComparer<T> : IComparer<T>
|
|
{
|
|
/// <summary>
|
|
/// 比较两个字符串,如果含用数字,则数字按数字的大小来比较。
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <returns></returns>
|
|
public int Compare(T x, T y)
|
|
{
|
|
if (x == null || y == null)
|
|
{
|
|
throw new ArgumentException("Parameters can't be null");
|
|
}
|
|
|
|
string fileA = x as string;
|
|
string fileB = y as string;
|
|
char[] arr1 = fileA.ToCharArray();
|
|
char[] arr2 = fileB.ToCharArray();
|
|
int i = 0, j = 0;
|
|
while (i < arr1.Length && j < arr2.Length)
|
|
{
|
|
if (char.IsDigit(arr1[i]) && char.IsDigit(arr2[j]))
|
|
{
|
|
string s1 = "", s2 = "";
|
|
while (i < arr1.Length && char.IsDigit(arr1[i]))
|
|
{
|
|
s1 += arr1[i];
|
|
i++;
|
|
}
|
|
while (j < arr2.Length && char.IsDigit(arr2[j]))
|
|
{
|
|
s2 += arr2[j];
|
|
j++;
|
|
}
|
|
if (int.Parse(s1) > int.Parse(s2))
|
|
{
|
|
return 1;
|
|
}
|
|
if (int.Parse(s1) < int.Parse(s2))
|
|
{
|
|
return -1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (arr1[i] > arr2[j])
|
|
{
|
|
return 1;
|
|
}
|
|
if (arr1[i] < arr2[j])
|
|
{
|
|
return -1;
|
|
}
|
|
i++;
|
|
j++;
|
|
}
|
|
}
|
|
if (arr1.Length == arr2.Length)
|
|
{
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
return arr1.Length > arr2.Length ? 1 : -1;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|