天天看點

c# IComparer比較字元串

例如:幻燈片50,幻燈片6,幻燈片40

排序完後:幻燈片6,幻燈片40,幻燈片50

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;
    }
}