luoyikun

导航

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

posted on 2017-09-09 21:09  luoyikun  阅读(11)  评论(0)    收藏  举报  来源