c# IComparer比较字符串

2023-08-24 13:45:17 浏览数 (1)

例如:幻灯片50,幻灯片6,幻灯片40 排序完后:幻灯片6,幻灯片40,幻灯片50

代码语言:javascript复制
public class PathCompare : IComparer<string>
{
    public int Compare(string x, string y)
    {
        string[] aBuf = x.Split('\');
        string[] bBuf = y.Split('\');
        string[] aNameBuf = aBuf[aBuf.Length - 1].Split('.');
        string[] bNameBuf = bBuf[bBuf.Length - 1].Split('.');
        if (aNameBuf[0].Length > bNameBuf[0].Length)
        {
            return 1;
        }
        else if (aNameBuf[0].Length < bNameBuf[0].Length)
        {
            return -1;
        }
        else if (aNameBuf[0].Length == bNameBuf[0].Length)
        {
            return aNameBuf[0].CompareTo(bNameBuf[0]);
        }
        return 0;
    }
}

0 人点赞