50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsmrManage
|
|
{
|
|
internal class NaturalOrderComparer : IComparer<string> //自然排序(自定义规则)
|
|
{
|
|
public int Compare(string? x, string? y)
|
|
{
|
|
// 提取字符串中的数字部分并将其转换为数字进行比较
|
|
int numX = ExtractNumber(x);
|
|
int numY = ExtractNumber(y);
|
|
|
|
return numX.CompareTo(numY);
|
|
}
|
|
|
|
private int ExtractNumber(string? input) // 截取数字部分
|
|
{
|
|
if (input == null)
|
|
{
|
|
return 0;
|
|
}
|
|
string number = string.Empty;
|
|
foreach (char c in input)
|
|
{
|
|
if (char.IsDigit(c))
|
|
{
|
|
number += c;
|
|
}
|
|
else if (number.Length > 0)
|
|
{
|
|
// 已经提取到数字部分,遇到非数字字符则停止提取
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (int.TryParse(number, out int result))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// 默认情况下,无法提取数字,则返回 0
|
|
return 0;
|
|
}
|
|
}
|
|
}
|